Вот реализация пользовательской ячейки выбора. Единственным изменением ванильного ксамарина является то, что отображение привязки должно вводиться как string
вместо binding: property
.
Вот весь важный код:
Декларации обязательного имущества
/// <summary>
/// The placeholder property.
/// </summary>
public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create(
nameof(Placeholder),
typeof(string),
typeof(CustomPickerCell),
propertyChanged: (bindable, oldVal, newVal) => ((CustomPickerCell)bindable).OnPlaceholderChanged((string)newVal)
);
/// <summary>
/// The items source property.
/// </summary>
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
nameof(ItemsSource),
typeof(IList),
typeof(CustomPickerCell),
propertyChanged: (bindable, oldVal, newVal) => ((CustomPickerCell)bindable).OnItemsSourceChanged((IList)newVal)
);
/// <summary>
/// The selected item property.
/// </summary>
public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create(
nameof(SelectedItem),
typeof(object),
typeof(CustomPickerCell),
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: (bindable, oldVal, newVal) => ((CustomPickerCell)bindable).OnSelectedItemChanged((object)newVal)
);
/// <summary>
/// The item display binding property
/// NOTE: Use the name of the property, you do not need to bind to this property.
/// </summary>
public static readonly BindableProperty ItemDisplayBindingProperty = BindableProperty.Create(
nameof(ItemDisplayBinding),
typeof(string),
typeof(CustomPickerCell),
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: (bindable, oldVal, newVal) => ((CustomPickerCell)bindable).OnItemDisplayBindingChanged((string)newVal)
);
Свойства привязки
/// <summary>
/// The cell's placeholder (select a ....).
/// </summary>
/// <value>The placeholder.</value>
public string Placeholder
{
get { return (string)GetValue(PlaceholderProperty); }
set { SetValue(PlaceholderProperty, value); }
}
/// <summary>
/// The cell's picker item source.
/// </summary>
/// <value>The items source.</value>
public IList ItemsSource
{
get { return (IList)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
/// <summary>
/// Gets or sets the selected item.
/// </summary>
/// <value>The selected item.</value>
public object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
/// <summary>
/// Gets or sets the item display binding.
/// </summary>
/// <value>The item display binding.</value>
public string ItemDisplayBinding
{
get { return (string)GetValue(ItemDisplayBindingProperty); }
set { SetValue(ItemDisplayBindingProperty, value); }
}
Методы, измененные свойством
/// <summary>
/// Called when PlaceholderProperty changes.
/// </summary>
/// <param name="newVal">New value.</param>
private void OnPlaceholderChanged(string newVal)
{
ItemPicker.Title = newVal;
}
/// <summary>
/// Called when ItemSourceProperty changes.
/// </summary>
private void OnItemsSourceChanged(IList list)
{
ItemPicker.ItemsSource = list;
}
/// <summary>
/// Called when SelectedItemProperty changes.
/// </summary>
/// <param name="obj">Object.</param>
private void OnSelectedItemChanged(object obj)
{
ItemPicker.SelectedItem = obj;
}
/// <summary>
/// Called when ItemDisplayBindingProperty changes.
/// </summary>
/// <param name="newvalue">Newvalue.</param>
private void OnItemDisplayBindingChanged(string newvalue)
{
ItemPicker.ItemDisplayBinding = new Binding(newvalue);
}
* 1023 инициализации *
public CustomPickerCell()
{
InitializeComponent();
ItemPicker.ItemsSource = this.ItemsSource;
ItemPicker.SelectedItem = this.SelectedItem;
ItemPicker.SelectedIndexChanged += OnSelectedIndexChanged;
}
/// <summary>
/// Calle when ItemPicker's SelectedIndexChanged event fires.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">E.</param>
void OnSelectedIndexChanged(object sender, EventArgs e)
{
this.SelectedItem = (ItemPicker.SelectedIndex < 0 || ItemPicker.SelectedIndex > ItemPicker.Items.Count - 1) ? null : ItemsSource[ItemPicker.SelectedIndex];
}
Теперь все, что вам нужно сделать, это добавить эту ячейку в ваш xaml, и все готово!
Usage
<!-- Where controls is a namespace defined in your xaml -->
<!-- Not not to bind ItemDisplayBinding, you only need the property name as a string-->
<controls:CustomPickerCell Title="Type" Placeholder="Select an ice cream"
ItemsSource="{Binding IceCreamList}"
SelectedItem="{Binding SelectedIceCream, Mode=TwoWay}"
ItemDisplayBinding="Name"/>