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
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.Core.Commands;
using System.ComponentModel;
namespace Tango.SharedUI.Controls
{
public class MultiSelectComboBox : Control
{
static MultiSelectComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(typeof(MultiSelectComboBox)));
}
public MultiSelectComboBox() : base()
{
RemoveAllCommand = new RelayCommand((() => SelectedItemsList.Clear()), () => SelectedItemsList != null && SelectedItemsList.Count > 0);
RemoveItemCommand = new RelayCommand<string>((item) => SelectedItemsList.Remove(item), () => SelectedItemsList != null);
AddItemCommand = new RelayCommand<string>((item) => SelectedItemsList.Add(item));
SearchText = "";
}
#region Properties
public ObservableCollection<string> SelectedItemsList
{
get { return (ObservableCollection<string>)GetValue(SelectedItemsListProperty); }
set { SetValue(SelectedItemsListProperty, value); }
}
public static readonly DependencyProperty SelectedItemsListProperty =
DependencyProperty.Register("SelectedItemsList", typeof(ObservableCollection<string>), typeof(MultiSelectComboBox), new PropertyMetadata(default(ObservableCollection<string>)));
public ListCollectionView SearchItemsList
{
get { return (ListCollectionView)GetValue(SearchItemsListProperty); }
set { SetValue(SearchItemsListProperty, value); }
}
/// <summary>
/// The search items list property for popup items list
/// </summary>
public static readonly DependencyProperty SearchItemsListProperty =
DependencyProperty.Register("SearchItemsList", typeof(ListCollectionView), typeof(MultiSelectComboBox), new PropertyMetadata(default(ObservableCollection<string>)));
public ObservableCollection<string> Items
{
get { return (ObservableCollection<string>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register("Items", typeof(ObservableCollection<string>), typeof(MultiSelectComboBox), new PropertyMetadata(default(ObservableCollection<string>)));
public bool IsToggleChecked
{
get { return (bool)GetValue(IsToggleCheckedProperty); }
set { SetValue(IsToggleCheckedProperty, value); }
}
public static readonly DependencyProperty IsToggleCheckedProperty =
DependencyProperty.Register("IsToggleChecked", typeof(bool), typeof(MultiSelectComboBox), new PropertyMetadata(false));
private string _searchText;
/// <summary>
/// Gets or sets the search text of TextBox.
/// </summary>
public string SearchText
{
get { return _searchText; }
set {_searchText = value;
RaisePropertyChanged("SearchText");
OnTextChanged();
}
}
void OnTextChanged()
{
if(SearchItemsList != null)
{
if (String.IsNullOrEmpty(SearchText))
SearchItemsList.Filter = null;
else
SearchItemsList.Filter = new Predicate<object>(o => ((string)o == SearchText));
}
}
/// <summary>
/// Occurs when a property has changed.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the property changed event.
/// </summary>
/// <param name="propName">Name of the property.</param>
protected virtual void RaisePropertyChanged(String propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
#endregion
/// <summary>
/// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate" />.
/// </summary>
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (SelectedItemsList == null)
SelectedItemsList = new ObservableCollection<string>();
SearchItemsList = new ListCollectionView(this.Items);
//ItemsControl iControl = FindName("asd") as ItemsControl;
//iControl.ItemContainerGenerator.ItemsChanged += ItemContainerGenerator_ItemsChanged;
//iControl.ItemContainerGenerator.ContainerFromItem(null);
//for (int i = 0; i < iControl.Items.Count; i++)
//{
// FrameworkElement a = iControl.ItemContainerGenerator.ContainerFromIndex(i) as FrameworkElement;
//}
}
private void ItemContainerGenerator_ItemsChanged(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs e)
{
}
#region Commands
/// <summary>
/// The remove all command property for clear SelectedItemsList
/// </summary>
public static DependencyProperty RemoveAllCommandProperty = DependencyProperty.Register("RemoveAllCommand", typeof(ICommand), typeof(MultiSelectComboBox));
public ICommand RemoveAllCommand
{
get { return (ICommand)GetValue(RemoveAllCommandProperty); }
private set { SetValue(RemoveAllCommandProperty, value); }
}
/// <summary>
/// The remove item from SelectedItemsList command property
/// </summary>
public static DependencyProperty RemoveItemCommandProperty = DependencyProperty.Register("RemoveItemCommand", typeof(RelayCommand<string>), typeof(MultiSelectComboBox));
public RelayCommand<string> RemoveItemCommand
{
get { return (RelayCommand<string>)GetValue(RemoveItemCommandProperty); }
private set { SetValue(RemoveItemCommandProperty, value); }
}
public static DependencyProperty AddItemCommandProperty = DependencyProperty.Register("AddItemCommand", typeof(RelayCommand<string>), typeof(MultiSelectComboBox));
public RelayCommand<string> AddItemCommand
{
get { return (RelayCommand<string>)GetValue(AddItemCommandProperty); }
private set { SetValue(AddItemCommandProperty, value); }
}
#endregion
}
}
|