This project has moved. For the latest updates, please go here.

Setting CategoryOrder via PropertyGrid PropertyDefinitions

Oct 19, 2016 at 2:22 AM
Hello,

I have a few property definitions in a propertygrid and I wondering how to set the order of the categories. Is this possible in XAML?

Example:
<xctk:PropertyGrid.PropertyDefinitions>
    <xctk:PropertyDefinition Name="Test3" Category="C3" CategoryOrder="1" />
    <xctk:PropertyDefinition Name="Test2" Category="C2" CategoryOrder="2" />
    <xctk:PropertyDefinition Name="Test1" Category="C1" CategoryOrder="3" />
</xctk:PropertyGrid.PropertyDefinitions>
(the "CategoryOrder" property is unavailable. Version 2.9.0)

Thanks.
Developer
Oct 21, 2016 at 3:53 PM
Hi,

Try setting attributes instead. You can set "CategoryAttribute" over each property to specify its Category and "CategoryOrderAttribute" over your object class (to specify the Categories order) which is your PropertyGrid's SelectedObject. Ex :
 <Grid>
    <xctk:PropertyGrid SelectedObject="{Binding}"
                       AutoGenerateProperties="False">
      <xctk:PropertyGrid.PropertyDefinitions>
        <xctk:PropertyDefinition Name="Test3"
                                 Category="C3"/>
        <xctk:PropertyDefinition Name="Test2"
                                 Category="C2" />
        <xctk:PropertyDefinition Name="Test1"
                                 Category="C1" />
      </xctk:PropertyGrid.PropertyDefinitions>
    </xctk:PropertyGrid>
  </Grid>
 public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      this.DataContext = new MyData();
    }
  }

  [CategoryOrder( "C1", 3) ]
  [CategoryOrder( "C2", 2 )]
  [CategoryOrder( "C3", 1 )]
  public class MyData
  {
   // [Category("C1")]   //Can be done as an attribute or in XAML
    public int Test1
    {
      get;
      set;
    }
   // [Category( "C2" )]   //Can be done as an attribute or in XAML
    public int Test2
    {
      get;
      set;
    }
   // [Category( "C3" )]   //Can be done as an attribute or in XAML
    public int Test3
    {
      get;
      set;
    }
    public int Test4
    {
      get;
      set;
    }
  }
Oct 27, 2016 at 1:57 AM
Does this solution work with application settings (Properties.Settings.Default)? I could not figure out how.
Developer
Oct 27, 2016 at 1:18 PM
Hi,

You cannot use "Properties.Settings.Default" in an attribute like :
[CategoryOrder( "C1", Properties.Settings.Default.... )
But if you define your setting in the "Settings.settings" file, like:
C1 int User 3
C2 int User 2
C3 int User 1

You can use reflection to add attributes of type "CategoryOrder" to your SelectedObject's class, like :
      var type = typeof( MyData );

      var aName = new System.Reflection.AssemblyName( "WpfApplication44" );
      var ab = AppDomain.CurrentDomain.DefineDynamicAssembly( aName, AssemblyBuilderAccess.Run );
      var mb = ab.DefineDynamicModule( aName.Name );
      var tb = mb.DefineType( type.Name, System.Reflection.TypeAttributes.Public, type );

      var attrCtorParams = new Type[] { typeof( string ), typeof( int ) };
      var attrCtorInfo = typeof( CategoryOrderAttribute ).GetConstructor( attrCtorParams );
      foreach( SettingsProperty prop in Properties.Settings.Default.Properties )
      {
        var attrBuilder = new CustomAttributeBuilder( attrCtorInfo, new object[] { prop.Name, Int32.Parse( prop.DefaultValue as string )} );
        tb.SetCustomAttribute( attrBuilder );
      }

      var newType = tb.CreateType();
      var instance = ( MyData )Activator.CreateInstance( newType );


      this.DataContext = instance;
――――
Get more controls, features, updates and technical support with Xceed Toolkit Plus for WPF