static void loadItemsAndSetSelected(BindableObject bindable) { ExtendedPicker picker = (ExtendedPicker)bindable; if (picker.ItemsSource as IEnumerable != null) { 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(); } picker.Items.Add(value); if (picker.SelectedItem != null) { if (picker.SelectedItem == obj) { picker.SelectedIndex = count; } } count++; } } }
private static void OnDisplayPropertyChanged(BindableObject bindable, object oldValue, object newValue) { ExtendedPicker picker = (ExtendedPicker)bindable; picker.DisplayProperty = (string)newValue; loadItemsAndSetSelected(bindable); }
private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue) { ExtendedPicker picker = (ExtendedPicker)bindable; picker.ItemsSource = (IList)newValue; loadItemsAndSetSelected(bindable); }
private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue) { ExtendedPicker picker = (ExtendedPicker)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++; } } }