我正在建立一些表格(使用TableView
)并注意到我对单元格的样式设置相同。我决定将这段重复的代码重构为一个公共控件。
I am struggling to get the binding to work on the picker correctly. My custom control looks like this:
我的控制是一个ViewCell
,这样我就可以在一个TableView
:
public partial class PickerCell : ViewCell
{
...
}
我已经能够设置选择器ItemSource
,但我无法得到SelectedItem
or DisplayItemBinding
属性来工作。
我见过这篇文章是2015年的 https://forums.xamarin.com/discussion/37933/bindable-picker-in-a-cell(现在已经很古老了)但我尝试过,但这些方法被标记为过时并且无论如何都不起作用。
我也在控件中尝试过:
ItemPicker.SetBinding(Picker.ItemsSourceProperty, "ItemSource");
ItemPicker.SetBinding(Picker.SelectedItemProperty, "SelectedItem", BindingMode.TwoWay);
但这也不起作用。
我基本上只是想添加一种方法,以便我可以将选择器从我的 xaml 绑定到控件。我真的希望这是可能的,因为我在我的应用程序周围使用这个精确的视图单元可能有 9/10 次。我真的不想重复太多,我通常会在这种情况下创建控件。例如,我有一个类似风格的条目单元格,并且工作完美......
这是我用来设置项目源的代码:
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
nameof(ItemsSource),
typeof(IList),
typeof(PickerCell)
propertyChanged: (bindable, oldVal, newVal) => ((PickerCell) bindable).OnItemsSourceChanged((IList) newVal)
);
public IList ItemsSource
{
get { return (IList)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
private void OnItemsSourceChanged(IList list)
{
ItemPicker.ItemsSource = list;
}
我尝试实现选择器的一些代码来自 Xamarin https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/Picker.cs但无济于事。
有任何想法吗?