Version

Properties List Sorting (xamPropertyGrid)

Topic Overview

Purpose

This topic explains how to provide logic for custom sorting of the properties list.

Required background

The following topics are prerequisites to understanding this topic:

Topic Purpose

This topic explains the features supported by the control from developer perspective.

This topic provides an overview of the visual elements of the control.

Sorting Summary

Sorting configuration summary

The xamPropertyGrid control supports declarative and programmatic ways for sorting (reordering) the properties list and its categories:

  • by specifying attributes on the property declarations

  • by specifying attributes for the categories on the declaration of the class containing the properties

  • by supplying a custom comparer for the properties

  • by supplying a custom comparer for the categories

Sorting configuration summary chart

The following table briefly explains the various options for sorting the properties list and maps them to the attributes/properties that enable them. Further details are available after the table.

Option Details Attribute/Property

Provides a declarative way to specify property sort order.

PropertySortOrder

Provides a declarative way to specify category sort order.

CategorySortOrder

Provides a programmatic way to specify property sort order.

Provides a programmatic way to specify category sort order.

Set Property Sort Order

Overview

You can customize the order in which properties are rendered by decorating a type’s property declarations with an attribute to indicate the sort order for each property.

Property settings

The following table maps the desired configuration to the property settings that manages it.

In order to: Use this attribute: And provide:

Set a property sort order

PropertySortOrder

A numerical value indicating the desired sort order for the property

Note
Note

The PropertySortOrder attribute is defined in the xamPropertyGrid’s assembly. If for some reason the classes where you need to use this attribute are not referencing the xamPropertyGrid’s assembly you can define your own attribute. Please note that in order for the xamPropertyGrid to recognize the attribute it should be named "PropertySortOrder" (or "PropertySortOrderAttribute") and it should have one parameter in the constructor of type int for the property sort order.

Example

The following code example demonstrates how to specify a sort order for three properties using attributes. Based on the attributes’ values, the Property Grid will display the three properties in the following order:

  • MyProperty2

  • MyProperty3

  • MyProperty1

In Visual Basic:

<PropertySortOrder(3)> _
Public Property MyProperty1() As String
  Get
    Return m_MyProperty1
  End Get
  Set
    m_MyProperty1 = Value
  End Set
End Property
Private m_MyProperty1 As String
<PropertySortOrder(1)> _
Public Property MyProperty2() As String
  Get
    Return m_MyProperty2
  End Get
  Set
    m_MyProperty2 = Value
  End Set
End Property
Private m_MyProperty2 As String
<PropertySortOrder(2)> _
Public Property MyProperty3() As String
  Get
    Return m_MyProperty3
  End Get
  Set
    m_MyProperty3 = Value
  End Set
End Property
Private m_MyProperty3 As String

In C#:

[PropertySortOrder(3)]
public string MyProperty1 { get; set; }
[PropertySortOrder(1)]
public string MyProperty2 { get; set; }
[PropertySortOrder(2)]
public string MyProperty3 { get; set; }

Set Category Sort Order

Overview

You can customize the sort order of categories by decorating the class containing the properties whose categories you want to sort with one or more CategorySortOrder attributes.

Property settings

The following table maps the desired configuration to the property settings that manage it.

In order to: Use this attribute: And provide:

Set a category sort order

CategorySortOrder

The category name and the numerical sort order

Note
Note

The CategorySortOrder attribute is defined in the xamPropertyGrid’s assembly. If for some reason the classes where you need to use this attribute are not referencing the xamPropertyGrid’s assembly you can define your own attribute. Please note that in order for the xamPropertyGrid to recognize the attribute it should be named "CategorySortOrder" (or "CategorySortOrderAttribute") and it should have two parameters in the constructor:

  • a parameter of type string for the category name

  • a parameter of type int for the sort order

Example

The following example demonstrates two categories each with two properties. The attributes set on the class will order the categories so that "Visual" is first and "Data" is second.

In Visual Basic:

<CategorySortOrder("Visual", 1)> _
<CategorySortOrder("Data", 2)> _
Public Class [MyClass]
  <Category("Data")> _
  Public Property DataSourceLocation() As String
    Get
      Return m_DataSourceLocation
    End Get
    Set
      m_DataSourceLocation = Value
    End Set
  End Property
  Private m_DataSourceLocation As String
  <Category("Data")> _
  Public Property DataSourcePath() As String
    Get
      Return m_DataSourcePath
    End Get
    Set
      m_DataSourcePath = Value
    End Set
  End Property
  Private m_DataSourcePath As String
  <Category("Visual")> _
  Public Property Foreground() As Color
    Get
      Return m_Foreground
    End Get
    Set
      m_Foreground = Value
    End Set
  End Property
  Private m_Foreground As Color
  <Category("Visual")> _
  Public Property Background() As String
    Get
      Return m_Background
    End Get
    Set
      m_Background = Value
    End Set
  End Property
  Private m_Background As String
End Class

In C#:

[CategorySortOrder("Visual", 1)]
[CategorySortOrder("Data", 2)]
public class MyClass
{
  [Category("Data")]
  public string DataSourceLocation { get; set; }
  [Category("Data")]
  public string DataSourcePath { get; set; }
  [Category("Visual")]
  public Color Foreground { get; set; }
  [Category("Visual")]
  public string Background { get; set; }
}

Provide Property Comparer

Overview

You can provide a custom property comparer which will be used by the control to sort the properties.

Property settings

The following table maps the desired configuration to the property that manages it.

In order to: Use this property: And set it to:

Provide a custom property comparer

PropertySortComparer

A type derived from IComparer<PropertyGridPropertyItem>

Example

The following code snippet demonstrates how to create a custom property comparer and set it on a Property Grid instance named "xamPropertyGrid1".

In Visual Basic:

Public Class MyCustomPropertyComparer
  Implements IComparer(Of PropertyGridPropertyItem)
  Public Function Compare(x As PropertyGridPropertyItem, y As PropertyGridPropertyItem) As Integer
    ' provide your custom comparing/sorting logic here
  End Function
End Class
…
Me.xamPropertyGrid1.PropertySortComparer = New MyCustomPropertyComparer()

In C#:

public class MyCustomPropertyComparer : IComparer<PropertyGridPropertyItem>
{
  public int Compare(PropertyGridPropertyItem x, PropertyGridPropertyItem y)
  {
    // provide your custom comparing/sorting logic here
  }
}
…
this.xamPropertyGrid1.PropertySortComparer = new MyCustomPropertyComparer();
Note
Note

There is a SortOrder property exposed on the PropertyGridPropertyItem which you may inspect in your IComparer implementation.

Provide Category Comparer

Overview

You can provide a custom category comparer which will be used by the control to sort the categories.

Property settings

The following table maps the desired configuration to the property that manages it.

In order to: Use this property: And set it to:

Provide a custom category comparer

CategorySortComparer

A type derived from IComparer<PropertyGridCategoryItem>

Example

The following code snippet demonstrates how to create a custom category comparer and set it on a Property Grid instance named "xamPropertyGrid1".

In Visual Basic:

Public Class MyCustomCategoryComparer
  Implements IComparer(Of PropertyGridCategoryItem)
  Public Function Compare(x As PropertyGridCategoryItem, y As PropertyGridCategoryItem) As Integer
    ' provide your custom comparing/sorting logic here
  End Function
End Class
…
Me.xamPropertyGrid1.CategorySortComparer = New MyCustomCategoryComparer()

In C#:

public class MyCustomCategoryComparer : IComparer<PropertyGridCategoryItem>
{
  public int Compare(PropertyGridCategoryItem x, PropertyGridCategoryItem y)
  {
    // provide your custom comparing/sorting logic here
  }
}
…
this.xamPropertyGrid1.CategorySortComparer = new MyCustomCategoryComparer();

Sorting Specifics

Specifics

  • If there are both attributes decorating the properties/categories in your object(s) and sort comparers supplied for properties/categories the comparers will take precedence.

  • Properties/categories with equal sort order will be sorted alphabetically by DisplayName.

  • Any property/category without sort order will default to a sort order of int.MaxValue which will cause them to be sorted alphabetically after the properties/categories with sort order specified.

Related Topics

The following topics provide additional information related to this topic.

Topic Purpose

This topic explains how to configure some general control options.

This topic explains how to customize the editors used for editing the properties’ values.

This topic explains how to configure the filtering of the control’s properties list.

This topic explains how create custom logic for assigning editing template on an editor definition.