private static void OnDisplayPropertyChanged(BindableObject bindable, object oldValue, object newValue) { BindablePicker picker = (BindablePicker)bindable; picker.DisplayProperty = (string)newValue; loadItemsAndSetSelected(bindable); }
private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue) { BindablePicker picker = (BindablePicker)bindable; picker.ItemsSource = (IList)newValue; loadItemsAndSetSelected(bindable); }
static void loadItemsAndSetSelected(BindableObject bindable) { BindablePicker picker = (BindablePicker)bindable; if (picker.ItemsSource as IEnumerable != null) { picker.disableEvents = true; picker.SelectedIndex = -1; picker.Items.Clear(); int count = 0; foreach (object obj in (IEnumerable)picker.ItemsSource) { string value = string.Empty; if (picker.DisplayProperty != null) { var prop = obj.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, picker.DisplayProperty, StringComparison.OrdinalIgnoreCase)); if (prop != null) { value = prop.GetValue(obj).ToString(); } else { value = obj.ToString(); } } else { value = obj.ToString(); } picker.Items.Add(value); if (picker.SelectedItem != null) { if (picker.SelectedItem == obj) { picker.SelectedIndex = count; } } count++; } picker.disableEvents = false; } else { picker.Items.Clear(); } }
private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue) { BindablePicker picker = (BindablePicker)bindable; picker.SelectedItem = newValue; if (picker.ItemsSource != null && picker.SelectedItem != null) { int count = 0; foreach (object obj in picker.ItemsSource) { if (obj == picker.SelectedItem) { picker.SelectedIndex = count; break; } count++; } } }