[英]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.