XCeed PropertyGrid - How to add a custom property with PropertyDefinition having bool (CheckBox) type

330 Views Asked by At

I use a PropertyGrid and I want to add a new property to keep the height and width ratio of any selected item. How can I add a new property having type bool (CheckBox)?

I tried this:

var propertyDefinition = new PropertyDefinition()
{
   Category = "Layout",
   DisplayName = "KeepRatio",
   TargetProperties = new string[] { "KeepRatio" },
   HasAttribute = typeof(Boolean)
};

propertyGrid1.PropertyDefinitions.Add(propertyDefinition);
1

There are 1 best solutions below

0
thatguy On

If your PropertyGrid is defined in XAML like this:

<xctk:PropertyGrid x:Name="propertyGrid1"/>

Then you can create the property definitions in code-behind like this:

// Create property definition, type is determined automatically.
var propertyDefinition = new PropertyDefinition()
{
   Category = "Layout",
   DisplayName = "KeepRatio",
   TargetProperties = new string[] { "KeepRatio" }
};

propertyGrid1.PropertyDefinitions.Add(propertyDefinition);

// Only show properties explicitly defined in PropertyDefinitions.
propertyGrid1.AutoGenerateProperties = false;

// Set the data object that contains the properties.
propertyGrid1.SelectedObject = this; // Replace "this" with your object.

However, you can do this also entirely in XAML, if you can bind your data object.

<xctk:PropertyGrid AutoGenerateProperties="False"
                   SelectedObject="{Binding YourDataObject}">
   <xctk:PropertyGrid.PropertyDefinitions>
      <xctk:PropertyDefinition Category="Layout"
                               DisplayName="KeepRatio"
                               TargetProperties="KeepRatio"/>
   </xctk:PropertyGrid.PropertyDefinitions>
</xctk:PropertyGrid>