【问题标题】:How do I use the BindingContext property of ViewCell in the XAML?如何在 XAML 中使用 ViewCell 的 BindingContext 属性?
【发布时间】: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


【解决方案1】:

您想为此使用DataTemplateSelector - 您并没有真正更改每个单元格的绑定上下文,而是更改了使用的可视单元格。 ListView 本身将控制这些 Binding 上下文。

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

【讨论】:

  • 感谢您的帮助,我查看了 DataTemplateSelector,它看起来是解决我问题的好方法。我通过在 ViewCell 上使用“BindingContextChanged”找到了另一种方法。我真正想通过这个实现的,是根据特定条件添加或删除 ContextActions
  • 我明白了,是的,这是一种有效的方法,而且我之前也为这个用例自己做过。
【解决方案2】:

对于我想要实现的目标,我做了以下...

在 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"
            });
        }
    }
}

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

【讨论】:

  • 此代码使应用程序在android平台崩溃请检查我的问题stackoverflow.com/questions/47618834/…
  • 老兄不要拒绝我的回答。您可能正在使用一个为您提供 NPE 的 android 设备,因为 LongPress 未在该设备上实现。使用不同的 API 版本。这将是一个 xamarin 错误
  • 我确认了自己。代码不会像@stepheaw 所说的那样崩溃。
【解决方案3】:

您可以直接使用 ViewCell 元素中的属性,如下所示。 ViewCell 不需要BindingContext

<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 有一个子类或自定义类,这种方法也应该有效。

【讨论】:

    猜你喜欢
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    相关资源
    最近更新 更多