Customize Properties
- 6 minutes to read
This topic shows how to customize cell editors, headers, and description in the Property Grid.
#Cell Editors
The PropertyGridControl creates cell editors based on property types.
public class Item {
public DateTime BirthDate { get; set; }
}
Use the following properties to display other content in property grid cells or additionally configure cell editors:
Member | Task |
---|---|
Assign and configure a cell editor | |
Display a different property value Display multiple property values in a single row |
#EditSettings
These approaches are compatible with all BaseEditSettings class descendants.
#Approach 1: Set the EditSettings property
Create a property definition and set the EditSettings property for it.
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<dxprg:PropertyDefinition Type="sys:DateTime">
<dxprg:PropertyDefinition.EditSettings>
<dxe:DateEditSettings DisplayFormat="MMM-dd-yyyy" />
</dxprg:PropertyDefinition.EditSettings>
</dxprg:PropertyDefinition>
#Approach 2: Use attributes or the DevExpress Fluent API
Create a template with a BaseEditSettings class descendant and use DevExpress.Mvvm.DataAnnotations.DefaultEditorAttribute, DevExpress.Mvvm.DataAnnotations.PropertyGridEditorAttribute, or the DevExpress Fluent API to specify this template key.
static MainWindow() {
MetadataLocator.Default = MetadataLocator.Create().AddMetadata<ItemMetadata>();
}
public class Item {
public DateTime Date { get; set; }
}
public class ItemMetadata : IMetadataProvider<Item> {
public void BuildMetadata(MetadataBuilder<Item> builder) {
builder.Property(x => x.Date).PropertyGridEditor("DateEditor");
}
}
<Window.Resources>
<DataTemplate x:Key="DateEditor">
<dxe:DateEditSettings Mask="dd-MM-yyyy" MaskUseAsDisplayFormat="True" />
</DataTemplate>
</Window.Resources>
#CellTemplate
The table below shows the key differences between the EditSettings and CellTemplate properties.
Edit | Cell | |
---|---|---|
Performance | Normal | Lower |
Display custom editors | ||
Editor settings | Editors in all properties associated with the current property definition have similar settings | Editors may have different settings, for example, depending on the current property value |
Priority | Standard | Higher |
To gain the best user experience, use a DevExpress editor and set the editor’s Name to ‘PART_Editor‘. For example:
<DataTemplate>
<dxe:DateEdit Name="PART_Editor" DisplayFormatString="MMM-dd-yyyy" />
</DataTemplate>
#Approach 1: Set the CellTemplate property
Create a property definition and set the CellTemplate property for it.
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<dxprg:PropertyDefinition Type="sys:DateTime">
<dxprg:PropertyDefinition.CellTemplate>
<DataTemplate>
<dxe:DateEdit Name="PART_Editor" DisplayFormatString="MMM-dd-yyyy" />
</DataTemplate>
</dxprg:PropertyDefinition.CellTemplate>
</dxprg:PropertyDefinition>
#Approach 2: Use attributes or the DevExpress Fluent API
Create a template with a DevExpress editor whose name is set to PART_Editor and use DevExpress.Mvvm.DataAnnotations.DefaultEditorAttribute, DevExpress.Mvvm.DataAnnotations.PropertyGridEditorAttribute, or the DevExpress Fluent API to specify this template key.
static MainWindow() {
MetadataLocator.Default = MetadataLocator.Create().AddMetadata<ItemMetadata>();
}
public class Item {
public DateTime Date { get; set; }
}
public class ItemMetadata : IMetadataProvider<Item> {
public void BuildMetadata(MetadataBuilder<Item> builder) {
builder.Property(x => x.Date).PropertyGridEditor("DateEditor");
}
}
<Window.Resources>
<DataTemplate x:Key="DateEditor">
<dxe:DateEdit Mask="dd-MM-yyyy" MaskUseAsDisplayFormat="True" />
</DataTemplate>
</Window.Resources>
#ContentTemplate
PropertyDefinitionBase.ContentTemplate should contain CellEditorPresenter controls with the Path property set. Set PathMode to manage where CellEditorPresenter should look for a property specified in Path. The table below describes the available options.
Value | Description |
---|---|
Relative (default) | The Cell |
Absolute | The Cell |
#Example 1: How to display multiple properties in a single row
This example demonstrates a custom template that displays multiple properties in a single PropertyGridControl cell.
Both the absolute and relative paths are specified for the cell editor presenter objects.
using System;
namespace DXSample {
public class CategoryAttributesViewModel {
public virtual Person Person { get; protected set; }
public CategoryAttributesViewModel() {
Person = new Person {
FirstName = "Anita",
LastName = "Benson",
Address = new Address {
AddressLine1 = "9602 South Main",
AddressLine2 = "Seattle, 77025, USA",
},
Phone = "7138638137",
};
}
}
public class Person {
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
public string Phone { get; set; }
}
public class Address {
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}
}
#Example 2: How to specify a custom editor for CellEditorPresenter
This example demonstrates how to specify a custom editor for CellEditorPresenter associated with the Phone property. For this, create a separate PropertyDefinition for the Phone property and set EditSettings/CellTemplate for it.
<dxprg:PropertyGridControl SelectedObject="{Binding Customer}" ShowProperties="WithPropertyDefinitions">
<dxprg:PropertyDefinition Path="Name" />
<dxprg:PropertyDefinition Path="Phone">
<dxprg:PropertyDefinition.ContentTemplate>
<DataTemplate>
<StackPanel>
<dxprg:CellEditorPresenter Path="Email" PathMode="Absolute" />
<dxprg:CellEditorPresenter Path="Phone" PathMode="Absolute" />
</StackPanel>
</DataTemplate>
</dxprg:PropertyDefinition.ContentTemplate>
</dxprg:PropertyDefinition>
<dxprg:PropertyDefinition Path="Email" Visibility="Collapsed">
<dxprg:PropertyDefinition.EditSettings>
<dxe:HyperlinkEditSettings AllowAutoNavigate="True" NavigationUrlFormat="mailto:{0}" />
</dxprg:PropertyDefinition.EditSettings>
</dxprg:PropertyDefinition>
</dxprg:PropertyGridControl>
public class Customer {
public String Name { get; set; }
public String Email { get; set; }
public String Phone { get; set; }
}
#Process User Actions
The PropertyGridControl includes the following events that allow you to specify how users can interact with editors:
Event | Description |
---|---|
Get |
Allows you to specify whether an action (key down, text input, or mouse left button click) activates the focused editor. |
Process |
Allows you to prohibit the focused editor to process an activation action. |
Get |
Allows you to specify whether an active editor responds to keys that a user presses. |
#Immediate Posting
The PropertyGridControl posts new values to its data source when a user presses Enter
or focuses another row. Set the PropertyDefinition.PostOnEditValueChanged property to true
to post data each time a user modifies a value.
#Headers
The Header is the leftmost element within a PropertyGrid row.
Use the following properties to affect the header.
Member | Characteristics |
---|---|
Content | |
Position | |
Template |
For example, property definitions below remove headers ([0], [1], and so on) from collection members and make the Name property header red.
<!-- xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib" -->
<dxprg:PropertyDefinition Type="{x:Type collections:IList}" TypeMatchMode="Extended">
<dxprg:PropertyDefinition Header="" />
</dxprg:PropertyDefinition>
<dxprg:PropertyDefinition Header="Name">
<dxprg:PropertyDefinition.HeaderTemplate>
<DataTemplate>
<TextBlock Foreground="Red" Text="{Binding Header}" />
</DataTemplate>
</dxprg:PropertyDefinition.HeaderTemplate>
</dxprg:PropertyDefinition>
#Description
The Description appears in a tooltip or a panel at the bottom of the Property Grid.
Use the following properties to change the description behavior or appearance.
Member | Characteristics |
---|---|
Content | |
Position | |
Template |
For example, the following code makes the description text red.
<dxprg:PropertyDefinition Description="Local number" Header="Home number" Path="HomeNumber">
<dxprg:PropertyDefinition.DescriptionTemplate>
<DataTemplate>
<TextBlock Foreground="Red" Text="{Binding}" />
</DataTemplate>
</dxprg:PropertyDefinition.DescriptionTemplate>
</dxprg:PropertyDefinition>