forked from Inspirare-LLC/Xamarin.Forms.ComboBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComboBox.cs
184 lines (151 loc) · 7.24 KB
/
ComboBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections;
using static Xamarin.Forms.VisualMarker;
namespace Xamarin.Forms.ComboBox
{
/// <summary>
/// Combo box with search option
/// </summary>
public class ComboBox : StackLayout
{
private Entry _entry;
private ListView _listView;
private bool _supressFiltering;
private bool _supressSelectedItemFiltering;
//Bindable properties
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable), typeof(ComboBox), defaultValue: null, propertyChanged: (bindable, oldVal, newVal) => {
var comboBox = (ComboBox)bindable;
comboBox._listView.ItemsSource = (IEnumerable)newVal;
});
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create(nameof(SelectedItem), typeof(object), typeof(ComboBox), defaultValue: null, propertyChanged: (bindable, oldVal, newVal) => {
var comboBox = (ComboBox)bindable;
comboBox._listView.SelectedItem = newVal;
});
public object SelectedItem
{
get { return (object)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
public static new readonly BindableProperty VisualProperty = BindableProperty.Create(nameof(Visual), typeof(IVisual), typeof(ComboBox), defaultValue: new DefaultVisual(), propertyChanged: (bindable, oldVal, newVal) => {
var comboBox = (ComboBox)bindable;
comboBox._listView.Visual = (IVisual)newVal;
comboBox._entry.Visual = (IVisual)newVal;
});
public new IVisual Visual
{
get { return (IVisual)GetValue(VisualProperty); }
set { SetValue(VisualProperty, value); }
}
public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create(nameof(Placeholder), typeof(string), typeof(ComboBox), defaultValue: "", propertyChanged: (bindable, oldVal, newVal) => {
var comboBox = (ComboBox)bindable;
comboBox._entry.Placeholder = (string)newVal;
});
public string Placeholder
{
get { return (string)GetValue(PlaceholderProperty); }
set { SetValue(PlaceholderProperty, value); }
}
public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(ComboBox), defaultValue: "", propertyChanged: (bindable, oldVal, newVal) => {
var comboBox = (ComboBox)bindable;
comboBox._entry.Text = (string)newVal;
});
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly BindableProperty ItemTemplateProperty = BindableProperty.Create(nameof(ItemTemplate), typeof(DataTemplate), typeof(ComboBox), defaultValue: null, propertyChanged: (bindable, oldVal, newVal) => {
var comboBox = (ComboBox)bindable;
comboBox._listView.ItemTemplate = (DataTemplate)newVal;
});
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
public static readonly BindableProperty EntryDisplayPathProperty = BindableProperty.Create(nameof(EntryDisplayPath), typeof(string), typeof(ComboBox), defaultValue: "");
public string EntryDisplayPath
{
get { return (string)GetValue(EntryDisplayPathProperty); }
set { SetValue(EntryDisplayPathProperty, value); }
}
public event EventHandler<SelectedItemChangedEventArgs> SelectedItemChanged;
protected virtual void OnSelectedItemChanged(SelectedItemChangedEventArgs e)
{
EventHandler<SelectedItemChangedEventArgs> handler = SelectedItemChanged;
handler?.Invoke(this, e);
}
public event EventHandler<TextChangedEventArgs> TextChanged;
protected virtual void OnTextChanged(TextChangedEventArgs e)
{
EventHandler<TextChangedEventArgs> handler = TextChanged;
handler?.Invoke(this, e);
}
public ComboBox()
{
//Entry used for filtering list view
_entry = new Entry();
_entry.Margin = new Thickness(0);
_entry.Keyboard = Keyboard.Create(KeyboardFlags.None);
_entry.Focused += (sender, args) => _listView.IsVisible = true;
_entry.Unfocused += (sender, args) => _listView.IsVisible = false;
//Text changed event, bring it back to the surface
_entry.TextChanged += (sender, args) =>
{
if (_supressFiltering)
return;
if (String.IsNullOrEmpty(args.NewTextValue))
{
_supressSelectedItemFiltering = true;
_listView.SelectedItem = null;
_supressSelectedItemFiltering = false;
}
_listView.IsVisible = true;
OnTextChanged(args);
};
//List view - used to display search options
_listView = new ListView();
_listView.Margin = new Thickness(0);
Xamarin.Forms.PlatformConfiguration.iOSSpecific.ListView.SetSeparatorStyle(_listView, Xamarin.Forms.PlatformConfiguration.iOSSpecific.SeparatorStyle.FullWidth);
_listView.HeightRequest = 100;
_listView.HorizontalOptions = LayoutOptions.StartAndExpand;
_listView.IsVisible = false;
_listView.SetBinding(ListView.SelectedItemProperty, new Binding(nameof(ComboBox.SelectedItem), source: this));
//Item selected event, surface it back to the top
_listView.ItemSelected += (sender, args) =>
{
if (!_supressSelectedItemFiltering)
{
_supressFiltering = true;
var selectedItem = args.SelectedItem;
_entry.Text = !String.IsNullOrEmpty(EntryDisplayPath) && selectedItem != null ? selectedItem.GetType().GetProperty(EntryDisplayPath).GetValue(selectedItem, null).ToString() : selectedItem?.ToString();
_supressFiltering = false;
_listView.IsVisible = false;
OnSelectedItemChanged(args);
}
};
//Add bottom border
var boxView = new BoxView();
boxView.HeightRequest = 1;
boxView.Color = Color.Black;
boxView.Margin = new Thickness(0);
boxView.SetBinding(BoxView.IsVisibleProperty, new Binding(nameof(ListView.IsVisible), source: _listView));
Children.Add(_entry);
Children.Add(_listView);
Children.Add(boxView);
}
public new bool Focus()
{
return _entry.Focus();
}
public new void Unfocus()
{
_entry.Unfocus();
}
}
}