diff options
Diffstat (limited to 'Xamarin.Forms.Core/Picker.cs')
-rw-r--r-- | Xamarin.Forms.Core/Picker.cs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/Picker.cs b/Xamarin.Forms.Core/Picker.cs new file mode 100644 index 00000000..733f2079 --- /dev/null +++ b/Xamarin.Forms.Core/Picker.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using Xamarin.Forms.Platform; + +namespace Xamarin.Forms +{ + [RenderWith(typeof(_PickerRenderer))] + public class Picker : View + { + public static readonly BindableProperty TitleProperty = BindableProperty.Create("Title", typeof(string), typeof(Picker), default(string)); + + public static readonly BindableProperty SelectedIndexProperty = BindableProperty.Create("SelectedIndex", typeof(int), typeof(Picker), -1, BindingMode.TwoWay, + propertyChanged: (bindable, oldvalue, newvalue) => + { + EventHandler eh = ((Picker)bindable).SelectedIndexChanged; + if (eh != null) + eh(bindable, EventArgs.Empty); + }, coerceValue: CoerceSelectedIndex); + + public Picker() + { + Items = new ObservableList<string>(); + ((ObservableList<string>)Items).CollectionChanged += OnItemsCollectionChanged; + } + + public IList<string> Items { get; } + + public int SelectedIndex + { + get { return (int)GetValue(SelectedIndexProperty); } + set { SetValue(SelectedIndexProperty, value); } + } + + public string Title + { + get { return (string)GetValue(TitleProperty); } + set { SetValue(TitleProperty, value); } + } + + public event EventHandler SelectedIndexChanged; + + static object CoerceSelectedIndex(BindableObject bindable, object value) + { + var picker = (Picker)bindable; + return picker.Items == null ? -1 : ((int)value).Clamp(-1, picker.Items.Count - 1); + } + + void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + { + SelectedIndex = SelectedIndex.Clamp(-1, Items.Count - 1); + } + } +}
\ No newline at end of file |