Skip to content

Instantly share code, notes, and snippets.

@adamped
Created November 23, 2015 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamped/5c0f8ba2511b35998f49 to your computer and use it in GitHub Desktop.
Save adamped/5c0f8ba2511b35998f49 to your computer and use it in GitHub Desktop.
public partial class BindablePicker : Picker
{
public BindablePicker()
{
InitializeComponent();
this.SelectedIndexChanged += OnSelectedIndexChanged;
}
public static BindableProperty ItemsSourceProperty =
BindableProperty.Create<BindablePicker, IList>(o => o.ItemsSource, default(IList), propertyChanged: OnItemsSourceChanged);
public static BindableProperty SelectedItemProperty =
BindableProperty.Create<BindablePicker, object>(o => o.SelectedItem, default(object), propertyChanged: UpdateSelected);
public string DisplayMember { get; set; }
public IList ItemsSource
{
get { return (IList)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public object SelectedItem
{
get { return (object)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
private static void OnItemsSourceChanged(BindableObject bindable, IList oldvalue, IList newvalue)
{
var picker = bindable as BindablePicker;
if (picker != null)
{
picker.Items.Clear();
if (newvalue == null) return;
foreach (var item in newvalue)
{
if (string.IsNullOrEmpty(picker.DisplayMember))
{
picker.Items.Add(item.ToString());
}
else
{
picker.Items.Add(picker.DisplayMember);
}
}
}
}
private void OnSelectedIndexChanged(object sender, EventArgs eventArgs)
{
if (SelectedIndex < 0 || SelectedIndex > Items.Count - 1)
{
SelectedItem = null;
}
else
{
SelectedItem = ItemsSource[SelectedIndex];
}
}
private static void UpdateSelected(BindableObject bindable, object oldvalue, object newvalue)
{
var picker = bindable as BindablePicker;
if (picker != null)
if (picker.ItemsSource != null)
if (picker.ItemsSource.Contains(picker.SelectedItem))
picker.SelectedIndex = picker.ItemsSource.IndexOf(picker.SelectedItem);
else
picker.SelectedIndex = -1;
}
}
@SagarPanwala
Copy link

How to change font size, text color and use custom font?

@taiful
Copy link

taiful commented Jun 20, 2017

How to change font size, text color and use custom font?

@adamped
Copy link
Author

adamped commented Oct 19, 2017

Just to note, that a BindablePicker exists in XF now, this was created before it existed, and is no longer needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment