xaml到C#代码中绑定的Xamarin ListView
如何将xaml到C#代码中绑定的Xamarin ListView,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,如何将xaml文件中带有ItemTemplate和Binding的以下列表视图转换为等效的C代码: 尝试以下方法: ListView lv = new ListView(); lv.HorizontalOptions = LayoutOptions.FillAndExpand; lv.VerticalOptions = LayoutOptions.FillAndExpand; lv.SetBinding(ListView.ItemsSourceProperty, new Binding("A")
xaml
文件中带有ItemTemplate
和Binding
的以下列表视图转换为等效的C代码:
尝试以下方法:
ListView lv = new ListView();
lv.HorizontalOptions = LayoutOptions.FillAndExpand;
lv.VerticalOptions = LayoutOptions.FillAndExpand;
lv.SetBinding(ListView.ItemsSourceProperty, new Binding("A"));
lv.ItemSelected += (sender, args) =>
{
onclick(sender, args);
}; //Remember to remove this event handler on dispoing of the page;
DataTemplate dt = new DataTemplate(typeof(TextCell));
dt.SetBinding(TextCell.TextProperty, new Binding("b"));
dt.SetBinding(TextCell.DetailProperty, new Binding("c"));
lv.ItemTemplate = dt;
对于更复杂的数据模板,请执行以下操作:
DataTemplate dt = new DataTemplate(() =>
{
var button = new Button();
button.SetBinding(Button.TextProperty, new Binding("Name"));
return new ViewCell { View = button };
});
数据模板中是否可能存在多个
TextCell
s?在这种情况下,如何正确绑定字段?@user1您能告诉我们如何在同一日期模板上有按钮和标签吗。。thanks@user1嘿,如果您使用stacklayout而不是textcell,并且stacklayout中有两个标签。如何知道要与哪个标签绑定dt.SetBinding(Label.TextProperty,new binding(“b”))
如何指定要绑定到哪个标签。或者甚至导航到复杂的数据模板,例如Grid?@夜光_Dev在您的情况下,dt
应该是要设置绑定的标签
DataTemplate dt = new DataTemplate(() =>
{
var button = new Button();
button.SetBinding(Button.TextProperty, new Binding("Name"));
return new ViewCell { View = button };
});