简体   繁体   English

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

[英]Xamarin Forms - Make custom cell bind to original listview itemsource when calling event "ItemSelected"

I have searched around and I dont think I am finding the answer to my question.我四处搜寻,但我认为我找不到问题的答案。 I am new to xamarin so i hope I am using the correct terminology.我是 xamarin 的新手,所以我希望我使用的是正确的术语。 I am experimenting with custom cells in listviews.我正在尝试在列表视图中使用自定义单元格。 My aim is to reuse the custom cell throughout multiple parts of my application but when I use the event "ItemSelected" it comes back with the bindings to the custom cell and not my original listview itemsource bindings.我的目标是在我的应用程序的多个部分中重用自定义单元格,但是当我使用事件“ItemSelected”时,它会返回到自定义单元格的绑定,而不是我原来的 listview itemsource 绑定。 I understand why I think but I am unsure how to bind the ItemSelected to the original source.我明白我为什么这么想,但我不确定如何将 ItemSelected 绑定到原始来源。 Am I using the right method here?我在这里使用正确的方法吗? I am completely lost if I am honest.老实说,我完全迷失了。

This is my custom cell code:这是我的自定义单元格代码:

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(string), 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
            };
        }
    }

Here is my ListView这是我的列表视图

<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>

Thank you very much in advance:)非常感谢您提前:)

According to your code, when binding to a custom cell type's BindableProperty instances, the UI controls displaying the BindableProperty values should use the OnBindingContextChanged override to set the data to be displayed in each cell.根据您的代码,当绑定到自定义单元格类型的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", typeof(string), typeof(ListCell),"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>

But You can use TextCell in ListView's DataTemplate directly, don't need to create custom viewcell.但是您可以直接在 ListView 的 DataTemplate 中使用TextCell ,不需要创建自定义 viewcell。

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

About using TextCell, you can take a look:关于使用TextCell,可以看一下:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin.Forms 中 ListView 上的 ItemTapped 和 ItemSelected 事件有什么区别? - What's the difference between the ItemTapped and the ItemSelected event on a ListView in Xamarin.Forms? 如何使用 MVVM 和 Xamarin.Forms ListView ItemSelected 事件来检索所选项目的绑定属性? - How do I use MVVM and Xamarin.Forms ListView ItemSelected event to to retrieve a bound property of the item selected? 绑定列表视图Xamarin表单中的自定义项目列表 - bind custom list of items in a listview Xamarin forms ItemSource 外部的 Xamarin 表单绑定命令 - Xamarin Forms Bind command outside ItemSource Xamarin.Forms (PRISM) - 将复选框绑定到 viewmodel 中的命令,在 listview itemsource 之外 - Xamarin.Forms (PRISM) - Bind checkbox, to a command in viewmodel, outside listview itemsource Xamarin Forms,使用async来应用ListView ItemSource - Xamarin Forms, using async to apply ListView ItemSource 在 ListView 上选择的项目以及有关 object 的更多信息(Xamarin.Forms、C #) - ItemSelected on ListView and more information about the object (Xamarin.Forms, C #) Xamarin Forms XAML 上的 ListView ItemTapped/ItemSelected 命令绑定 - Xamarin Forms ListView ItemTapped/ItemSelected Command Binding on XAML 将Web服务方法分配给Xamarin表单中的ListView的ItemSource - Assign Web Service method to ItemSource of ListView in Xamarin Forms 尽管修改了 Itemsource,MVVM ListView 仍未更新 | XAMARIN.FORMS, ANDROID - MVVM ListView not updating despite Itemsource modification | XAMARIN FORMS, ANDROID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM