如何在XAML中使用ViewCell的BindingContext属性?

本文关键字:ViewCell BindingContext 属性 XAML | 更新日期: 2023-09-27 17:53:08

嗨,我想使用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吗?

如何在XAML中使用ViewCell的BindingContext属性?

您希望为此使用DataTemplateSelector -您并没有真正更改每个单元格的Binding上下文,因为您正在更改使用的视觉单元格。ListView本身将控制这些Binding上下文。

https://blog.xamarin.com/customizing-list-view-cells-xamarin-forms-datatemplateselector/

对于我想要实现的目标,我做了以下操作…

在XAML 中
<ViewCell BindingContextChanged="OnBindingContextChanged">

后面的代码
private void OnBindingContextChanged(object sender, EventArgs e)
{
    base.OnBindingContextChanged();
    if (BindingContext == null)
        return;
    ViewCell theViewCell = ((ViewCell)sender);
    var item = theViewCell.BindingContext as ListItemModel;
    theViewCell.ContextActions.Clear();
    if (item != null)
    {
        if (item.ListItemType == ListItemTypeEnum.FavoritePlaces
           || item.ListItemType == ListItemTypeEnum.FavoritePeople)
        {
            theViewCell.ContextActions.Add(new MenuItem()
            {
                Text = "Delete"
            });
        }
    }
}

根据我们正在处理的列表项的类型,我们可以决定在哪里放置上下文动作

你可以直接在ViewCell的元素中使用属性,如下所示:BindingContext不需要ViewCell.

<ListView ItemsSource="{Binding Attachments}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <StackLayout>
          <Label Text="{Binding Name}" />
          <Image Source="{Binding FilePath}"/>
        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

确保不要使用ViewCell.View作为ViewCell的子元素。

这种方法应该工作,即使你有一个子类或自定义类的ViewCell