Ghost1372

everything can be handy

PropertyGrid

It can automatically generate a matching editor collection for the attributes in the model object.

1
2
3
[TemplatePart(Name = ElementItemsControl, Type = typeof(ItemsControl))]
[TemplatePart(Name = ElementSearchBar, Type = typeof(SearchBar))]
public class PropertyGrid : Control

Attributes

Property Description Default Value Remarks
PropertyResolver Property Resolver new PropertyResolver
SelectedObject Model Object
Description Description
MaxTitleWidth Max Title Width 0
MinTitleWidth Minimum title width 0

Bring your own editor

Name Description
DatePropertyEditor Date Editor
DateTimePropertyEditor Date Time Editor
EnumPropertyEditor Enumeration Editor
HorizontalAlignmentPropertyEditor Horizontal Alignment Editor
ImagePropertyEditor Image Editor
NumberPropertyEditor Number Editor
PlainTextPropertyEditor Plain Text Editor
ReadOnlyTextPropertyEditor Read-only text editor
SwitchPropertyEditor Boolean editor (switch style)
TimePropertyEditor Time Editor
VerticalAlignmentPropertyEditor Vertical Alignment Editor

Events

Name Description
SelectedObjectChanged Triggered when the model object changes

Case

Basic usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class PropertyGridDemoModel
{
[Category("Category1")]
public string String { get; set; }
[Category("Category2")]
public int Integer { get; set; }
[Category("Category2")]
public bool Boolean { get; set; }
[Category("Category1")]
public Gender Enum { get; set; }
public HorizontalAlignment HorizontalAlignment { get; set; }
public VerticalAlignment VerticalAlignment { get; set; }
public ImageSource ImageSource { get; set; }
}
public enum Gender
{
Male,
Female
}
1
2
3
4
5
6
7
8
DemoModel = new PropertyGridDemoModel
{
String = "TestString",
Enum = Gender.Female,
Boolean = true,
Integer = 98,
VerticalAlignment = VerticalAlignment.Stretch
};
1
<hc:PropertyGrid Width="500" SelectedObject="{Binding DemoModel}"/>

PropertyGrid

Custom editor

Let’s take PlainTextPropertyEditor as an example. When we need to customize the editor, we can inherit from PropertyEditorBase and override some methods. The definitions of these methods are shown in the following table:

Methods

Name Description Remarks
CreateElement Create specific operation controls Must be rewritten
CreateBinding Create data binding for specific operation controls
GetDependencyProperty Get the dependency property that needs to be bound in the specific operation control Must be rewritten
GetBindingMode Get binding mode
GetUpdateSourceTrigger Get the trigger mode of updating the data source
GetConverter Get the converter to be used when binding

The specific operation control of the plain text editor can be TextBox:

1
2
3
4
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.TextBox
{
IsReadOnly = propertyItem.IsReadOnly
};

The dependency property that needs to be bound should be TextProperty:

1
public override DependencyProperty GetDependencyProperty() => System.Windows.Controls.TextBox.TextProperty;

The final overall code is as follows:

1
2
3
4
5
6
7
8
public class PlainTextPropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.TextBox
{
IsReadOnly = propertyItem.IsReadOnly
};
public override DependencyProperty GetDependencyProperty() => System.Windows.Controls.TextBox.TextProperty;
}
0%