-
Notifications
You must be signed in to change notification settings - Fork 913
Expand file tree
/
Copy pathObjectContainerHelperBase.cs
More file actions
559 lines (458 loc) · 18.2 KB
/
Copy pathObjectContainerHelperBase.cs
File metadata and controls
559 lines (458 loc) · 18.2 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*************************************************************************************
Toolkit for WPF
Copyright (C) 2007-2018 Xceed Software Inc.
This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
For more features, controls, and fast professional support,
pick up the Plus Edition at https://xceed.com/xceed-toolkit-plus-for-wpf/
Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
***********************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using Xceed.Wpf.Toolkit.Core.Utilities;
using Xceed.Wpf.Toolkit.PropertyGrid.Editors;
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows.Controls.Primitives;
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
using System.Windows.Controls;
namespace Xceed.Wpf.Toolkit.PropertyGrid
{
internal abstract class ObjectContainerHelperBase : ContainerHelperBase
{
// This is needed to work around the ItemsControl behavior.
// When ItemsControl is preparing its containers, it appears
// that calling Refresh() on the CollectionView bound to
// the ItemsSource prevents the items from being displayed.
// This patch is to avoid such a behavior.
private bool _isPreparingItemFlag = false;
private PropertyItemCollection _propertyItemCollection;
public ObjectContainerHelperBase( IPropertyContainer propertyContainer)
: base( propertyContainer )
{
_propertyItemCollection = new PropertyItemCollection( new ObservableCollection<PropertyItem>() );
UpdateFilter();
UpdateCategorization( false );
}
public override IList Properties
{
get { return _propertyItemCollection; }
}
private PropertyItem DefaultProperty
{
get
{
PropertyItem defaultProperty = null;
var defaultName = this.GetDefaultPropertyName();
if( defaultName != null )
{
defaultProperty = _propertyItemCollection
.FirstOrDefault( ( prop ) => object.Equals( defaultName, prop.PropertyDescriptor.Name ) );
}
return defaultProperty;
}
}
protected PropertyItemCollection PropertyItems
{
get
{
return _propertyItemCollection;
}
}
public override PropertyItemBase ContainerFromItem( object item )
{
if( item == null )
return null;
// Exception case for ObjectContainerHelperBase. The "Item" may sometimes
// be identified as a string representing the property name or
// the PropertyItem itself.
Debug.Assert( item is PropertyItem || item is string );
var propertyItem = item as PropertyItem;
if( propertyItem != null )
return propertyItem;
var propertyStr = item as string;
if( propertyStr != null )
return PropertyItems.FirstOrDefault( ( prop ) => propertyStr == prop.PropertyDescriptor.Name );
return null;
}
public override object ItemFromContainer( PropertyItemBase container )
{
// Since this call is only used to update the PropertyGrid.SelectedProperty property,
// return the PropertyName.
var propertyItem = container as PropertyItem;
if( propertyItem == null )
return null;
return propertyItem.PropertyDescriptor.Name;
}
public override void UpdateValuesFromSource()
{
foreach( PropertyItem item in PropertyItems )
{
item.DescriptorDefinition.UpdateValueFromSource();
item.ContainerHelper.UpdateValuesFromSource();
}
}
public void GenerateProperties()
{
if( (PropertyItems.Count == 0)
)
{
this.RegenerateProperties();
}
}
protected override void OnFilterChanged()
{
this.UpdateFilter();
}
protected override void OnCategorizationChanged()
{
UpdateCategorization( true );
}
protected override void OnAutoGeneratePropertiesChanged()
{
this.RegenerateProperties();
}
protected override void OnHideInheritedPropertiesChanged()
{
this.RegenerateProperties();
}
protected override void OnEditorDefinitionsChanged()
{
this.RegenerateProperties();
}
protected override void OnPropertyDefinitionsChanged()
{
this.RegenerateProperties();
}
protected internal override void SetPropertiesExpansion( bool isExpanded )
{
if( this.Properties.Count == 0 )
{
this.GenerateProperties();
}
base.SetPropertiesExpansion( isExpanded );
}
protected internal override void SetPropertiesExpansion( string propertyName, bool isExpanded )
{
if( this.Properties.Count == 0 )
{
this.GenerateProperties();
}
base.SetPropertiesExpansion( propertyName, isExpanded );
}
private void UpdateFilter()
{
FilterInfo filterInfo = this.PropertyContainer.FilterInfo;
this.PropertyItems.FilterPredicate = filterInfo.Predicate
?? PropertyItemCollection.CreateFilter( filterInfo.InputString, this.PropertyItems, this.PropertyContainer );
}
private void UpdateCategorization( bool updateSubPropertiesCategorization )
{
_propertyItemCollection.UpdateCategorization( this.ComputeCategoryGroupDescription(), this.PropertyContainer.IsCategorized, this.PropertyContainer.IsSortedAlphabetically );
if( updateSubPropertiesCategorization && (_propertyItemCollection.Count > 0) )
{
foreach( PropertyItem propertyItem in _propertyItemCollection )
{
PropertyItemCollection subPropertyItemsCollection = propertyItem.Properties as PropertyItemCollection;
if( subPropertyItemsCollection != null )
{
subPropertyItemsCollection.UpdateCategorization( this.ComputeCategoryGroupDescription(), this.PropertyContainer.IsCategorized, this.PropertyContainer.IsSortedAlphabetically );
}
}
}
}
private GroupDescription ComputeCategoryGroupDescription()
{
if( !PropertyContainer.IsCategorized )
return null;
return new PropertyGroupDescription( PropertyItemCollection.CategoryPropertyName );
}
private string GetCategoryGroupingPropertyName()
{
var propGroup = this.ComputeCategoryGroupDescription() as PropertyGroupDescription;
return ( propGroup != null ) ? propGroup.PropertyName : null;
}
private void OnChildrenPropertyChanged( object sender, PropertyChangedEventArgs e )
{
if( ObjectContainerHelperBase.IsItemOrderingProperty( e.PropertyName )
|| this.GetCategoryGroupingPropertyName() == e.PropertyName )
{
// Refreshing the view while Containers are generated will throw an exception
if( this.ChildrenItemsControl.ItemContainerGenerator.Status != GeneratorStatus.GeneratingContainers
&& !_isPreparingItemFlag )
{
PropertyItems.RefreshView();
}
}
}
protected abstract string GetDefaultPropertyName();
protected abstract void GenerateSubPropertiesCore( Action<IEnumerable<PropertyItem>> updatePropertyItemsCallback );
private void RegenerateProperties()
{
this.GenerateSubPropertiesCore( this.UpdatePropertyItemsCallback );
}
private void UpdatePropertyItemsCallback( IEnumerable<PropertyItem> subProperties )
{
foreach( var propertyItem in subProperties )
{
this.InitializePropertyItem( propertyItem );
}
//Remove the event callback from the previous children (if any)
foreach( var propertyItem in PropertyItems )
{
propertyItem.PropertyChanged -= OnChildrenPropertyChanged;
}
PropertyItems.UpdateItems( subProperties );
//Add the event callback to the new childrens
foreach( var propertyItem in PropertyItems )
{
propertyItem.PropertyChanged += OnChildrenPropertyChanged;
}
// Update the selected property on the property grid only.
PropertyGrid propertyGrid = PropertyContainer as PropertyGrid;
if( propertyGrid != null )
{
propertyGrid.SelectedPropertyItem = this.DefaultProperty;
}
}
protected static List<PropertyDescriptor> GetPropertyDescriptors( object instance, bool hideInheritedProperties )
{
PropertyDescriptorCollection descriptors = null;
TypeConverter tc = TypeDescriptor.GetConverter( instance );
if( tc == null || !tc.GetPropertiesSupported() )
{
if( instance is ICustomTypeDescriptor )
{
descriptors = ((ICustomTypeDescriptor)instance).GetProperties();
}
//ICustomTypeProvider is only available in .net 4.5 and over. Use reflection so the .net 4.0 and .net 3.5 still works.
else if( instance.GetType().GetInterface( "ICustomTypeProvider", true ) != null )
{
var methodInfo = instance.GetType().GetMethod( "GetCustomType" );
var result = methodInfo.Invoke( instance, null ) as Type;
descriptors = TypeDescriptor.GetProperties( result );
}
else
{
descriptors = TypeDescriptor.GetProperties( instance.GetType() );
}
}
else
{
try
{
descriptors = tc.GetProperties( instance );
}
catch( Exception )
{
}
}
if( ( descriptors != null ) )
{
var descriptorsProperties = descriptors.Cast<PropertyDescriptor>();
if( hideInheritedProperties )
{
var properties = from p in descriptorsProperties
where p.ComponentType == instance.GetType()
select p;
return properties.ToList();
}
else
{
return descriptorsProperties.ToList();
}
}
return null;
}
protected bool GetWillRefreshPropertyGrid( PropertyDescriptor propertyDescriptor )
{
if( propertyDescriptor == null )
return false;
var attribute = PropertyGridUtilities.GetAttribute<RefreshPropertiesAttribute>( propertyDescriptor );
if( attribute != null )
return attribute.RefreshProperties != RefreshProperties.None;
return false;
}
internal void InitializeDescriptorDefinition(
DescriptorPropertyDefinitionBase descriptorDef,
PropertyDefinition propertyDefinition )
{
if( descriptorDef == null )
throw new ArgumentNullException( "descriptorDef" );
if( propertyDefinition == null )
return;
// Values defined on PropertyDefinition have priority on the attributes
if( propertyDefinition != null )
{
if( propertyDefinition.Category != null )
{
descriptorDef.Category = propertyDefinition.Category;
descriptorDef.CategoryValue = propertyDefinition.Category;
}
if( propertyDefinition.Description != null )
{
descriptorDef.Description = propertyDefinition.Description;
}
if( propertyDefinition.DisplayName != null )
{
descriptorDef.DisplayName = propertyDefinition.DisplayName;
}
if( propertyDefinition.DisplayOrder != null )
{
descriptorDef.DisplayOrder = propertyDefinition.DisplayOrder.Value;
}
if( propertyDefinition.IsExpandable != null )
{
descriptorDef.ExpandableAttribute = propertyDefinition.IsExpandable.Value;
}
}
}
private void InitializePropertyItem( PropertyItem propertyItem )
{
DescriptorPropertyDefinitionBase pd = propertyItem.DescriptorDefinition;
propertyItem.PropertyDescriptor = pd.PropertyDescriptor;
propertyItem.IsReadOnly = pd.IsReadOnly;
propertyItem.DisplayName = pd.DisplayName;
propertyItem.Description = pd.Description;
propertyItem.Category = pd.Category;
propertyItem.PropertyOrder = pd.DisplayOrder;
//These properties can vary with the value. They need to be bound.
if( pd.PropertyDescriptor.Converter is ExpandableObjectConverter )
{
propertyItem.IsExpandable = true;
}
else
{
SetupDefinitionBinding( propertyItem, PropertyItemBase.IsExpandableProperty, pd, () => pd.IsExpandable, BindingMode.OneWay );
}
SetupDefinitionBinding( propertyItem, PropertyItemBase.AdvancedOptionsIconProperty, pd, () => pd.AdvancedOptionsIcon, BindingMode.OneWay );
SetupDefinitionBinding( propertyItem, PropertyItemBase.AdvancedOptionsTooltipProperty, pd, () => pd.AdvancedOptionsTooltip, BindingMode.OneWay );
SetupDefinitionBinding( propertyItem, PropertyItem.ValueProperty, pd, () => pd.Value, BindingMode.TwoWay );
if( pd.CommandBindings != null )
{
foreach( CommandBinding commandBinding in pd.CommandBindings )
{
propertyItem.CommandBindings.Add( commandBinding );
}
}
}
private object GetTypeDefaultValue( Type type )
{
if( type.IsGenericType && type.GetGenericTypeDefinition() == typeof( Nullable<> ) )
{
type = type.GetProperty( "Value" ).PropertyType;
}
return ( type.IsValueType ? Activator.CreateInstance( type ) : null ) ;
}
private void SetupDefinitionBinding<T>(
PropertyItem propertyItem,
DependencyProperty itemProperty,
DescriptorPropertyDefinitionBase pd,
Expression<Func<T>> definitionProperty,
BindingMode bindingMode )
{
string sourceProperty = ReflectionHelper.GetPropertyOrFieldName( definitionProperty );
Binding binding = new Binding( sourceProperty )
{
Source = pd,
Mode = bindingMode
};
propertyItem.SetBinding( itemProperty, binding );
}
internal FrameworkElement GenerateChildrenEditorElement( PropertyItem propertyItem )
{
FrameworkElement editorElement = null;
DescriptorPropertyDefinitionBase pd = propertyItem.DescriptorDefinition;
object definitionKey = null;
Type definitionKeyAsType = definitionKey as Type;
ITypeEditor editor = null;
if( editor == null )
editor = pd.CreateAttributeEditor();
if( editor != null )
editorElement = editor.ResolveEditor( propertyItem );
if( (editorElement == null) && (definitionKey == null) && ( propertyItem.PropertyDescriptor != null ) )
editorElement = this.GenerateCustomEditingElement( propertyItem.PropertyDescriptor.Name, propertyItem );
if( editorElement == null && definitionKeyAsType == null )
editorElement = this.GenerateCustomEditingElement( propertyItem.PropertyType, propertyItem );
if( editorElement == null )
{
if( pd.IsReadOnly )
editor = new ReadOnlyTextBoxEditor();
// Fallback: Use a default type editor.
if( editor == null )
{
editor = ( definitionKeyAsType != null )
? PropertyGridUtilities.CreateDefaultEditor( definitionKeyAsType, null, propertyItem )
: pd.CreateDefaultEditor( propertyItem );
}
Debug.Assert( editor != null );
editorElement = editor.ResolveEditor( propertyItem );
}
return editorElement;
}
internal PropertyDefinition GetPropertyDefinition( PropertyDescriptor descriptor )
{
PropertyDefinition def = null;
var propertyDefs = this.PropertyContainer.PropertyDefinitions;
if( propertyDefs != null )
{
def = propertyDefs[ descriptor.Name ];
if( def == null )
{
def = propertyDefs.GetRecursiveBaseTypes( descriptor.PropertyType );
}
}
return def;
}
public override void PrepareChildrenPropertyItem( PropertyItemBase propertyItem, object item )
{
_isPreparingItemFlag = true;
base.PrepareChildrenPropertyItem( propertyItem, item );
if( propertyItem.Editor == null )
{
FrameworkElement editor = this.GenerateChildrenEditorElement( ( PropertyItem )propertyItem );
if( editor != null )
{
// Tag the editor as generated to know if we should clear it.
ContainerHelperBase.SetIsGenerated( editor, true );
propertyItem.Editor = editor;
}
}
_isPreparingItemFlag = false;
}
public override void ClearChildrenPropertyItem( PropertyItemBase propertyItem, object item )
{
if( propertyItem.Editor != null
&& ContainerHelperBase.GetIsGenerated( propertyItem.Editor ) )
{
propertyItem.Editor = null;
}
base.ClearChildrenPropertyItem( propertyItem, item );
}
public override Binding CreateChildrenDefaultBinding( PropertyItemBase propertyItem )
{
Binding binding = new Binding( "Value" );
binding.Mode = ( ( ( PropertyItem )propertyItem ).IsReadOnly ) ? BindingMode.OneWay : BindingMode.TwoWay;
return binding;
}
protected static string GetDefaultPropertyName( object instance )
{
AttributeCollection attributes = TypeDescriptor.GetAttributes( instance );
DefaultPropertyAttribute defaultPropertyAttribute = ( DefaultPropertyAttribute )attributes[ typeof( DefaultPropertyAttribute ) ];
return defaultPropertyAttribute != null ? defaultPropertyAttribute.Name : null;
}
private static bool IsItemOrderingProperty( string propertyName )
{
return string.Equals( propertyName, PropertyItemCollection.DisplayNamePropertyName )
|| string.Equals( propertyName, PropertyItemCollection.CategoryOrderPropertyName )
|| string.Equals( propertyName, PropertyItemCollection.PropertyOrderPropertyName );
}
}
}