Skip to main content

Properties Panel

  • 7 minutes to read

The Properties panel displays properties of an object that is selected in the canvas and allows users to edit them.

Properties_panel

Users can toggle the auto-hide mode or close the panel. To show the panel, right-click a diagram item or empty space in the canvas and select Properties in the menu. You can also use the Ribbon UI (View > Panes) to show or hide the Properties panel.

wpf diagram panes

The table below lists the main customization options.

Characteristics Members
Availability DiagramControl.AllowPropertiesPanel
Visibility DiagramControl.PropertiesPanelVisibility
Measure Unit DiagramControl.MeasureUnit
Display measure unit next to values DiagramControl.ShowMeasureUnit

Add Properties

To add a property to the Properties panel, handle the CustomGetEditableItemProperties event. The following code snippet illustrates how to add the DiagramItem.CanSnapToThisItem property for shapes:

private void Diagram_CustomGetEditableItemProperties(object sender, DiagramCustomGetEditableItemPropertiesEventArgs e) {
    if (e.Item is DiagramShape) {
        e.Properties.Add(TypeDescriptor.GetProperties(typeof(DiagramShape))["CanSnapToThisItem"]);
    }
}

You can add properties from a custom DiagramItem descendant and other sources, such as objects stored in a DataContext.

Add a Shape Descendant’s Property

To add a property from a DiagramShape descendant, use the TypeDescriptor.GetProperties method to obtain the PropertyDescriptor and add it to the DiagramItemCreatingEventArgs.Properties collection:

public class CustomDiagramShape : DiagramShape {
    public string Info {
        get;
        set;
    }
} 

private void DiagramDesignerControl_CustomGetEditableItemProperties(object sender, DevExpress.Xpf.Diagram.DiagramCustomGetEditableItemPropertiesEventArgs e) {
    if (e.Item is CustomDiagramShape)
    {
        PropertyDescriptor infoPropertyDescriptor = TypeDescriptor.GetProperties(typeof(CustomDiagramShape))["Info"];
        e.Properties.Add(infoPropertyDescriptor);
    }
}

The Properties panel uses the INotifyPropertyChanged interface for notifications about updates in the source object. To enable synchronization when the Info property is modified, implement the INotifyPropertyChanged interface in the CustomDiagramShape class and raise the PropertyChanged event in the Info property setter.

Note

Use the DevExpress.Diagram.Core.DiagramItemTypeRegistrator.Register method to register diagram item descendants in a DiagramControl. To use custom diagram item types instead of the regular ones, handle the DiagramControl.ItemCreating event.

Add Independent Properties

The Properties panel allows users to edit properties that are not defined directly at the DiagramItem level. This can be useful when your diagram is bound to a data source and you need to edit data item properties. For this purpose, DiagramCustomGetEditableItemPropertiesEventArgs provides the CreateProxyProperty method that allows you to create a custom property descriptor.

Add the property descriptor to the DiagramCustomGetEditableItemPropertiesEventArgs.Properties collection to edit a custom property. For instance, you can use the following code to add the Customer.Name property to the Properties panel if the DiagramShape‘s DataContext contains a Customer class instance:

private void DiagramDesignerControl_CustomGetEditableItemProperties(object sender,
        DevExpress.Xpf.Diagram.DiagramCustomGetEditableItemPropertiesEventArgs e) {
    if (e.Item is DiagramShape)
    {
        PropertyDescriptor namePropertyDescriptor = e.CreateProxyProperty("Name", item => ((Customer)item.DataContext).Name, (item, value) => ((Customer)item.DataContext).Name = value);
        e.Properties.Add(namePropertyDescriptor);
    }
}

To add a property to a custom category, use the DisplayAttribute:

private void diagram_CustomGetEditableItemProperties(object sender, DiagramCustomGetEditableItemPropertiesEventArgs e) {
    if (e.Item is DiagramShape)
        e.Properties.Add(e.CreateProxyProperty("Custom Content", item => ((DiagramShape)item).Content,
            (item, value) => ((DiagramShape)item).Content = value, new Attribute[] { new DisplayAttribute() { GroupName = "Custom Group" } }));
}

Remove Properties

Tip

If you use protection options to restrict user actions, the corresponding properties are automatically removed from the Properties panel. For example, the Width and Height properties are removed from the panel if you set the DiagramControl.AllowResizeItems property to false. The diagram protection options are listed in the DiagramControl.IsReadOnly property’s description.

To remove a property from the Properties panel, handle the DiagramControl.CustomGetEditableItemProperties and remove the corresponding property descriptor from the Properties collection:

private void DiagramDesignerControl_CustomGetEditableItemProperties(object sender,
        DevExpress.Xpf.Diagram.DiagramCustomGetEditableItemPropertiesEventArgs e) {
    if (e.Item is DiagramShape)
    {
        e.Properties.Remove(e.Properties["Content"]);
    }
}

Customize Properties

Customize Property Names

To customize a property name, handle the DiagramControl.CustomGetEditableItemProperties event. Create a custom property descriptor wrapper with the CreateProxyProperty method and use the DisplayNameAttribute attribute.

The example below illustrates how to rename the Content property.

private void DiagramDesignerControl_CustomGetEditableItemProperties(object sender,
        DevExpress.Xpf.Diagram.DiagramCustomGetEditableItemPropertiesEventArgs e) {
    if (e.Item is DiagramShape)
    {
        Attribute[] additionalAttriutes = new Attribute[] { new DisplayNameAttribute("Custom content") };
        PropertyDescriptor contentPropertyDescriptorWrapper = e.CreateProxyProperty(e.Properties["Content"], item => item, additionalAttriutes);
        e.Properties.Remove(e.Properties["Content"]);
        e.Properties.Add(contentPropertyDescriptorWrapper);
    }
}

Customize Property Editors

To use a custom control to edit a property, create a property descriptor wrapper with the CreateProxyProperty method and pass an instance of PropertyGridEditorAttribute to this wrapper. Add this attribute to the property descriptor wrapper and define a template for your editor in diagram resources. The example below illustrates how to use a ComboBoxEdit to edit the diagram item’s content.

private void DiagramDesignerControl_CustomGetEditableItemProperties(object sender,
        DevExpress.Xpf.Diagram.DiagramCustomGetEditableItemPropertiesEventArgs e) {
    if (e.Item is DiagramShape)
    {
        Attribute[] additionalAttriutes = new Attribute[] { new PropertyGridEditorAttribute() { TemplateKey = "contentComboBoxEditor" } };
        PropertyDescriptor contentPropertyDescriptorWrapper = e.CreateProxyProperty(e.Properties["Content"], item => item, additionalAttriutes);
        e.Properties.Remove(e.Properties["Content"]);
        e.Properties.Add(contentPropertyDescriptorWrapper);
    }
}
<dxdiag:DiagramDesignerControl CustomGetEditableItemProperties="DiagramDesignerControl_CustomGetEditableItemProperties">
    <dxdiag:DiagramDesignerControl.Resources>
        <DataTemplate x:Key="contentComboBoxEditor">
            <dxe:ComboBoxEdit Name="PART_Editor">
                <dxe:ComboBoxEditItem Content="Director"/>
                <dxe:ComboBoxEditItem Content="Manager"/>
            </dxe:ComboBoxEdit>
        </DataTemplate>
    </dxdiag:DiagramDesignerControl.Resources>
</dxdiag:DiagramDesignerControl>

The PropertyGridControl locates your template based on the specified TemplateKey. The editor’s name should be set to PART_Editor.

Use Different Properties for Items of the Same Type

The DiagramControl caches property descriptors for items of the same type. Handle the CustomGetEditableItemPropertiesCacheKey event to conditionally choose properties for diagram items of the same type.

Validation

The PropertyGridControl supports attribute-based validation. This type of validation allows you to define validation rules for edited properties. The following code snippet adds a validation attribute that prevents users from typing a text string longer than five characters:

private void DiagramDesignerControl_CustomGetEditableItemProperties(object sender,
        DevExpress.Xpf.Diagram.DiagramCustomGetEditableItemPropertiesEventArgs e) {
    if (e.Item is DiagramShape)
    {
        Attribute[] additionalAttriutes = new Attribute[] { new System.ComponentModel.DataAnnotations.StringLengthAttribute(5) };
        PropertyDescriptor contentPropertyDescriptorWrapper = e.CreateProxyProperty(e.Properties["Content"], item => item, additionalAttriutes);
        e.Properties.Remove(e.Properties["Content"]);
        e.Properties.Add(contentPropertyDescriptorWrapper);
    }
}

You can use the following attributes for validation:

Note: You should add a reference to the System.ComponentModel.DataAnnotations assembly to use these attributes.

Create a Custom Property Panel

Use the PropertyGridControl to create a custom Property Panel. Bind the PropertyGridControl.SelectedObject property to the DiagramControl.SelectionModel property to display the properties of selected diagram items:

 <Window ...
        xmlns:dxdiag="http://schemas.devexpress.com/winfx/2008/xaml/diagram" 
        xmlns:dxprg="http://schemas.devexpress.com/winfx/2008/xaml/propertygrid">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="179"/>
        </Grid.ColumnDefinitions>
        <dxdiag:DiagramControl x:Name="diagramControl" Grid.Column="0" SelectedStencils="BasicShapes">
        </dxdiag:DiagramControl>
        <dxprg:PropertyGridControl Grid.Column="1"
            dxdiag:DiagramControl.Diagram="{Binding ElementName=diagramControl}
            SelectedObject="{Binding ElementName=diagramControl, Path=SelectionModel}"/>
    </Grid>
</Window>

Handle the CustomGetEditableItemProperties event to modify the list of diagram item properties displayed in the PropertyGridControl.

See Also