【发布时间】:2016-06-29 21:28:38
【问题描述】:
您好,我想根据特定条件使用BindingContext 属性将不同的ViewCell 绑定到我的Listview
这是 Xaml
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell BindingContext="??">//What do I do here?
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这里是 ViewCells 的类
public class textViewCellNoContextActions : ViewCell
{
public textViewCellNoContextActions()
{
StackLayout layout = new StackLayout();
layout.Padding = new Thickness(15, 0);
Label label = new Label();
label.SetBinding(Label.TextProperty, "ListItemTitle");
layout.Children.Add(label);
View = layout;
}
}
public class textViewCellWithContextActions : ViewCell
{
public textViewCellWithContextActions()
{
StackLayout layout = new StackLayout();
layout.Padding = new Thickness(15, 0);
Label label = new Label();
label.SetBinding(Label.TextProperty, "ListItemTitle");
layout.Children.Add(label);
var moreAction = new MenuItem { Text = "More" };
moreAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
moreAction.Clicked += OnMore;
var deleteAction = new MenuItem { Text = "Delete", IsDestructive = true }; // red background
deleteAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
deleteAction.Clicked += OnDelete;
this.ContextActions.Add(moreAction);
this.ContextActions.Add(deleteAction);
View = layout;
}
在我的ViewModel 中,我想决定绑定到哪个ViewCell。我如何实现这一目标?
我还需要使用BindingContextChanged吗?
【问题讨论】:
-
为什么要将列表中的项目绑定到列表源中的项目以外的内容?这似乎违背了拥有数据绑定列表的目的。
标签: c# xaml listview data-binding xamarin