forked from HandyOrg/HandyControl
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPropertyGrid.cs
More file actions
281 lines (231 loc) · 11.9 KB
/
Copy pathPropertyGrid.cs
File metadata and controls
281 lines (231 loc) · 11.9 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using HandyControl.Data;
using HandyControl.Data.Enum;
using HandyControl.Interactivity;
using HandyControl.Tools.Extension;
namespace HandyControl.Controls
{
[TemplatePart(Name = ElementItemsControl, Type = typeof(ItemsControl))]
[TemplatePart(Name = ElementSearchBar, Type = typeof(SearchBar))]
public class PropertyGrid : Control
{
private const string ElementItemsControl = "PART_ItemsControl";
private const string ElementSearchBar = "PART_SearchBar";
private ItemsControl _itemsControl;
private ICollectionView _dataView;
private SearchBar _searchBar;
private string _searchKey;
public PropertyGrid()
{
CommandBindings.Add(new CommandBinding(ControlCommands.SortByCategory, SortByCategory, (s, e) => e.CanExecute = ShowSortButton));
CommandBindings.Add(new CommandBinding(ControlCommands.SortByName, SortByName, (s, e) => e.CanExecute = ShowSortButton));
}
public virtual PropertyResolver PropertyResolver { get; } = new();
public static readonly RoutedEvent SelectedObjectChangedEvent =
EventManager.RegisterRoutedEvent("SelectedObjectChanged", RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<object>), typeof(PropertyGrid));
public event RoutedPropertyChangedEventHandler<object> SelectedObjectChanged
{
add => AddHandler(SelectedObjectChangedEvent, value);
remove => RemoveHandler(SelectedObjectChangedEvent, value);
}
public static readonly DependencyProperty SelectedObjectProperty = DependencyProperty.Register(
"SelectedObject", typeof(object), typeof(PropertyGrid), new PropertyMetadata(default, OnSelectedObjectChanged));
private static void OnSelectedObjectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctl = (PropertyGrid) d;
ctl.OnSelectedObjectChanged(e.OldValue, e.NewValue);
}
public object SelectedObject
{
get => GetValue(SelectedObjectProperty);
set => SetValue(SelectedObjectProperty, value);
}
protected virtual void OnSelectedObjectChanged(object oldValue, object newValue)
{
UpdateItems(newValue);
RaiseEvent(new RoutedPropertyChangedEventArgs<object>(oldValue, newValue, SelectedObjectChangedEvent));
}
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
"Description", typeof(string), typeof(PropertyGrid), new PropertyMetadata(default(string)));
public string Description
{
get => (string) GetValue(DescriptionProperty);
set => SetValue(DescriptionProperty, value);
}
public static readonly DependencyProperty MaxTitleWidthProperty = DependencyProperty.Register(
"MaxTitleWidth", typeof(double), typeof(PropertyGrid), new PropertyMetadata(ValueBoxes.Double0Box));
public double MaxTitleWidth
{
get => (double) GetValue(MaxTitleWidthProperty);
set => SetValue(MaxTitleWidthProperty, value);
}
public static readonly DependencyProperty MinTitleWidthProperty = DependencyProperty.Register(
"MinTitleWidth", typeof(double), typeof(PropertyGrid), new PropertyMetadata(ValueBoxes.Double0Box));
public double MinTitleWidth
{
get => (double) GetValue(MinTitleWidthProperty);
set => SetValue(MinTitleWidthProperty, value);
}
public static readonly DependencyProperty ShowSortButtonProperty = DependencyProperty.Register(
"ShowSortButton", typeof(bool), typeof(PropertyGrid), new PropertyMetadata(ValueBoxes.TrueBox));
public bool ShowSortButton
{
get => (bool) GetValue(ShowSortButtonProperty);
set => SetValue(ShowSortButtonProperty, value);
}
public static readonly DependencyProperty ShowSearchBarProperty = DependencyProperty.Register(
"ShowSearchBar", typeof(bool), typeof(PropertyGrid), new PropertyMetadata(ValueBoxes.TrueBox));
public bool ShowSearchBar
{
get => (bool) GetValue(ShowSearchBarProperty);
set => SetValue(ShowSearchBarProperty, value);
public static readonly DependencyProperty FlattenChildPropertiesProperty = DependencyProperty.Register(
"FlattenChildProperties", typeof(Flattening), typeof(PropertyGrid), new PropertyMetadata(Flattening.Off));
public Flattening FlattenChildProperties
{
get => (Flattening) GetValue(FlattenChildPropertiesProperty);
set => SetValue(FlattenChildPropertiesProperty, value);
}
public override void OnApplyTemplate()
{
if (_searchBar != null)
{
_searchBar.SearchStarted -= SearchBar_SearchStarted;
}
base.OnApplyTemplate();
_itemsControl = GetTemplateChild(ElementItemsControl) as ItemsControl;
_searchBar = GetTemplateChild(ElementSearchBar) as SearchBar;
if (_searchBar != null)
{
_searchBar.SearchStarted += SearchBar_SearchStarted;
}
UpdateItems(SelectedObject);
}
/// <summary>
/// Algorithmic helper class to temporarily link parent data to a PropertyDescriptorCollection
/// </summary>
private class ParentPropertyDescriptorCollection
{
public ParentPropertyDescriptorCollection(PropertyDescriptorCollection properties, string category)
{
Properties = properties;
Category = category;
}
public PropertyDescriptorCollection Properties { get; }
public string Category { get; }
}
private IEnumerable<PropertyItem> FlattenUnknownProperties(PropertyDescriptorCollection propertiesToFlatten, string parentCategory)
{
var browsableProperties = propertiesToFlatten.OfType<PropertyDescriptor>()
.Where(item => PropertyResolver.ResolveIsBrowsable(item)).ToList();
var knownProperties = browsableProperties.Where(item => PropertyResolver.IsKnownEditorType(item.PropertyType))
.Select(item => CreatePropertyItem(item, parentCategory))
.Do(item => item.InitElement());
var unknownPropertiesCollections = browsableProperties.Where(item => !PropertyResolver.IsKnownEditorType(item.PropertyType))
.Select(GetCategorizedChildProperties);
return unknownPropertiesCollections
.Select(coll => FlattenUnknownProperties(coll.Properties, coll.Category))
.Aggregate(knownProperties, (current, flattenedChildProperties) => current.Concat(flattenedChildProperties));
}
private ParentPropertyDescriptorCollection GetCategorizedChildProperties(PropertyDescriptor parentItem)
{
string category = null;
switch (FlattenChildProperties)
{
case Flattening.ParentCategory:
category = PropertyResolver.ResolveCategory(parentItem);
break;
case Flattening.ParentNameAsCategory:
category = parentItem.DisplayName;
break;
}
return new ParentPropertyDescriptorCollection(parentItem.GetChildProperties(), category);
}
private void UpdateItems(object obj)
{
if (obj == null || _itemsControl == null) return;
if (FlattenChildProperties == Flattening.Off)
{
_dataView = CollectionViewSource.GetDefaultView(TypeDescriptor.GetProperties(obj.GetType())
.OfType<PropertyDescriptor>()
.Where(item => PropertyResolver.ResolveIsBrowsable(item))
.Select(item => CreatePropertyItem(item, null))
.Do(item => item.InitElement()));
}
else
{
_dataView = CollectionViewSource.GetDefaultView(FlattenUnknownProperties(TypeDescriptor.GetProperties(obj.GetType()), null));
}
SortByCategory(null, null);
_itemsControl.ItemsSource = _dataView;
}
private void SortByCategory(object sender, ExecutedRoutedEventArgs e)
{
if (_dataView == null) return;
using (_dataView.DeferRefresh())
{
_dataView.GroupDescriptions.Clear();
_dataView.SortDescriptions.Clear();
_dataView.SortDescriptions.Add(new SortDescription(PropertyItem.CategoryProperty.Name, ListSortDirection.Ascending));
_dataView.SortDescriptions.Add(new SortDescription(PropertyItem.DisplayNameProperty.Name, ListSortDirection.Ascending));
_dataView.GroupDescriptions.Add(new PropertyGroupDescription(PropertyItem.CategoryProperty.Name));
}
}
private void SortByName(object sender, ExecutedRoutedEventArgs e)
{
if (_dataView == null) return;
using (_dataView.DeferRefresh())
{
_dataView.GroupDescriptions.Clear();
_dataView.SortDescriptions.Clear();
_dataView.SortDescriptions.Add(new SortDescription(PropertyItem.PropertyNameProperty.Name, ListSortDirection.Ascending));
}
}
private void SearchBar_SearchStarted(object sender, FunctionEventArgs<string> e)
{
if (_dataView == null) return;
_searchKey = e.Info;
if (string.IsNullOrEmpty(_searchKey))
{
foreach (UIElement item in _dataView)
{
item.Show();
}
}
else
{
foreach (PropertyItem item in _dataView)
{
item.Show(item.PropertyName.ToLower().Contains(_searchKey) || item.DisplayName.ToLower().Contains(_searchKey));
}
}
}
protected virtual PropertyItem CreatePropertyItem(PropertyDescriptor propertyDescriptor, string category) =>
new()
{
Category = category ?? PropertyResolver.ResolveCategory(propertyDescriptor),
DisplayName = PropertyResolver.ResolveDisplayName(propertyDescriptor),
Description = PropertyResolver.ResolveDescription(propertyDescriptor),
IsReadOnly = PropertyResolver.ResolveIsReadOnly(propertyDescriptor),
DefaultValue = PropertyResolver.ResolveDefaultValue(propertyDescriptor),
Editor = PropertyResolver.ResolveEditor(propertyDescriptor),
Value = SelectedObject,
PropertyName = propertyDescriptor.Name,
PropertyType = propertyDescriptor.PropertyType,
PropertyTypeName = $"{propertyDescriptor.PropertyType.Namespace}.{propertyDescriptor.PropertyType.Name}"
};
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
TitleElement.SetTitleWidth(this, new GridLength(Math.Max(MinTitleWidth, Math.Min(MaxTitleWidth, ActualWidth / 3))));
}
}
}