Xamarin Forms - 调用事件“ItemSelected”时使自定义单元格绑定到原始列表视图项源

我四处搜索,但我认为我找不到问题的答案。我是 xamarin 的新手,所以我希望我使用的是正确的术语。我正在尝试在列表视图中使用自定义单元格。我的目标是在我的应用程序的多个部分重用自定义单元格,但是当我使用事件“ItemSelected”时,它会返回到自定义单元格的绑定,而不是我原来的列表视图 itemsource 绑定。我明白我为什么这么想,但我不确定如何将 ItemSelected 绑定到原始源。我在这里使用正确的方法吗?老实说,我完全迷失了。

这是我的自定义单元格代码:

public partial class ListCell : ViewCell
    {
        public static readonly BindableProperty LabelHeaderProperty = BindableProperty.Create("LabelHeader",typeof(string),typeof(ListCell));
        public string LabelHeader
        {
            get { return (string)Getvalue(LabelHeaderProperty); }
            set { Setvalue(LabelHeaderProperty,value); }
        }
        public static readonly BindableProperty LabelSmallProperty = BindableProperty.Create("LabelSmall",typeof(ListCell));
        public string LabelSmall
        {
            get { return (string)Getvalue(LabelSmallProperty); }
            set { Setvalue(LabelSmallProperty,value); }
        }
        public ListCell()
        {
            InitializeComponent();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            BindingContext = new
            {
                LabelHeader = this.LabelHeader,LabelSmall = this.LabelSmall
            };
        }
    }

这是我的列表视图

<ListView x:Name="MyListView"
                ItemsSource="{Binding Items}"
                VerticalOptions="FillAndExpand"
                HasUnevenRows="true"
                IsPullToRefreshEnabled="true"
                ItemSelected="OnItemSelected"
                SeparatorVisibility="None">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <extensions:ListCell LabelHeader="{Binding Description}"
                                         LabelSmall="{Binding Description}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

非常感谢您 :)

nothingzz 回答:Xamarin Forms - 调用事件“ItemSelected”时使自定义单元格绑定到原始列表视图项源

根据您的代码,当绑定到自定义单元格类型的 BindableProperty 实例时,显示 BindableProperty 值的 UI 控件应使用 OnBindingContextChanged 覆盖来设置要显示的数据每个单元格。

 public class ListCell:ViewCell
{
    Label headerLabel,smallLabel;
    public static readonly BindableProperty LabelHeaderProperty = BindableProperty.Create("LabelHeader",typeof(string),typeof(ListCell),"name");
    public string LabelHeader
    {
        get { return (string)GetValue(LabelHeaderProperty); }
        set { SetValue(LabelHeaderProperty,value); }
    }
    public static readonly BindableProperty LabelSmallProperty = BindableProperty.Create("LabelSmall","small label");
    public string LabelSmall
    {
        get { return (string)GetValue(LabelSmallProperty); }
        set { SetValue(LabelSmallProperty,value); }
    }

    public ListCell()
    {
        StackLayout stack = new StackLayout { Orientation=StackOrientation.Horizontal};
        headerLabel = new Label { FontAttributes = FontAttributes.Bold };
        smallLabel = new Label();
        stack.Children.Add(headerLabel);
        stack.Children.Add(smallLabel);
        View = stack;
    }
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();

        if (BindingContext != null)
        {
            headerLabel.Text = LabelHeader;
            smallLabel.Text = LabelSmall;
           
        }
    }
}

 <ListView
            x:Name="listView"
            ItemSelected="listView_ItemSelected"
            ItemsSource="{Binding items}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <local:ListCell LabelHeader="{Binding Name}" LabelSmall="{Binding description}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

但是你可以直接在ListView的DataTemplate中使用TextCell,不需要创建自定义viewcell。

<ListView ItemsSource="{Binding items}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Detail="{Binding description}" Text="{Binding name}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

关于TextCell的使用,可以看一下:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/data-and-databinding#binding-cells

本文链接:https://www.f2er.com/320004.html

大家都在问