2 using System.Collections;
3 using System.Collections.ObjectModel;
4 using System.Collections.Specialized;
5 using System.ComponentModel;
9 namespace Tizen.Xamarin.Forms.Extension
12 /// The DropdownList is a button like widget that pops up a list of items(automatically choosing the direction to display) that have a string label.
13 /// It is a convenient widget to avoid the need to do all the piecing together yourself.
14 /// It is intended for a small number of items in the DropdownList menu (no more than 8), though, it is capable of many more.
18 /// var dropdownList = new DropdownList();
19 /// dropdownList.ItemSelected += (s, e) =>
21 /// Debug.WriteLine("e.Selected Item: " + e.SelectedItem);
24 /// ObservableCollection<string> ItemsList = new ObservableCollection<string>()
26 /// "item5", "item6", "item7", "item8"
29 /// dropdownList.ItemsSource = ItemsList;
32 public class DropdownList : View
35 /// BindableProperty. Identifies the SelectedItem bindable property.
37 public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create("SelectedItem", typeof(object), typeof(DropdownList), default(object), BindingMode.TwoWay, propertyChanged: OnSelectedItemChanged);
39 [Obsolete("IsHorizontalProperty is obsolete as of version 2.3.5-r256. The orientation is always vertical.")]
40 public static readonly BindableProperty IsHorizontalProperty = BindableProperty.Create("IsHorizontal", typeof(bool), typeof(DropdownList), default(bool));
42 [EditorBrowsable(EditorBrowsableState.Never)]
43 public static readonly BindablePropertyKey IsExpandedPropertyKey = BindableProperty.CreateReadOnly("IsExpanded", typeof(bool), typeof(DropdownList), default(bool), propertyChanged: OnIsExpandedPropertyChanged);
46 /// BindableProperty. Identifies the IsExpanded bindable property.
48 public static readonly BindableProperty IsExpandedProperty = IsExpandedPropertyKey.BindableProperty;
51 /// BindableProperty. Identifies the ItemsSource bindable property.
53 public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(DropdownList), default(IEnumerable), propertyChanged: OnItemsSourceChanged);
56 /// BindableProperty. Identifies the DisplayMemberPath bindable property.
58 public static readonly BindableProperty DisplayMemberPathProperty = BindableProperty.Create("DisplayMemberPath", typeof(string), typeof(DropdownList), default(string));
61 /// Occurs when an item in the DropdownList is selected.
62 /// SelectedItemChangedEventArgs will also have the selected item.
64 public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
67 /// Occurs when the DropdownList is expanded.
69 public event EventHandler Expanded;
72 /// Occurs when the DropdownList is collapsed.
74 public event EventHandler Collapsed;
76 [EditorBrowsable(EditorBrowsableState.Never)]
77 public event EventHandler<ExpandRequestArgs> ExpandChangeRequested;
80 /// Raised when one or more items of ItemsSource is changed.
82 public event NotifyCollectionChangedEventHandler CollectionChanged;
85 /// Gets or sets the currently selected item from the DropdownList.ItemsSource.
87 public object SelectedItem
89 get { return (object)GetValue(SelectedItemProperty); }
90 set { SetValue(SelectedItemProperty, value); }
93 [Obsolete("IsHorizontal is obsolete as of version 2.3.5-r256. The orientation is always vertical.")]
94 public bool IsHorizontal
96 get { return (bool)GetValue(IsHorizontalProperty); }
97 set { SetValue(IsHorizontalProperty, value); }
101 /// Gets the flag of whether the dropdownlist is expanded.
103 public bool IsExpanded
105 get { return (bool)GetValue(IsExpandedProperty); }
109 /// Gets or sets the source of items to template and display.
111 public IEnumerable ItemsSource
113 get { return (IEnumerable)GetValue(ItemsSourceProperty); }
114 set { SetValue(ItemsSourceProperty, value); }
118 /// Gets or sets a member path to a value on the ItemsSource.
120 public string DisplayMemberPath
122 get { return (string)GetValue(DisplayMemberPathProperty); }
123 set { SetValue(DisplayMemberPathProperty, value); }
127 /// Expands the DropdownList popup from code.
133 var arg = new ExpandRequestArgs { IsExpand = true };
134 ExpandChangeRequested?.Invoke(this, arg);
139 /// Collapses the DropdownList popup from code.
141 public void Collapse()
145 var arg = new ExpandRequestArgs();
146 ExpandChangeRequested?.Invoke(this, arg);
150 static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
152 (bindable as DropdownList).OnItemsSourceChanged(oldValue as INotifyCollectionChanged, newValue as INotifyCollectionChanged);
155 void OnItemsSourceChanged(INotifyCollectionChanged oldValue, INotifyCollectionChanged newValue)
157 if (oldValue != null)
159 oldValue.CollectionChanged -= OnNotifyCollectionChanged;
161 if (newValue != null)
163 newValue.CollectionChanged += OnNotifyCollectionChanged;
167 void OnNotifyCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
169 CollectionChanged?.Invoke(this, e);
172 static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
174 var dropdownList = (DropdownList)bindable;
175 dropdownList.ItemSelected?.Invoke(bindable, new SelectedItemChangedEventArgs(newValue));
178 static void OnIsExpandedPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
180 var element = bindable as DropdownList;
183 var isExpanded = (bool)newvalue;
186 element.Expanded?.Invoke(element, EventArgs.Empty);
190 element.Collapsed?.Invoke(element, EventArgs.Empty);
195 [EditorBrowsable(EditorBrowsableState.Never)]
196 public class ExpandRequestArgs : EventArgs
198 public bool IsExpand { get; set; }