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

Sample code to populate fonts and colors in property grid ?

Aug 27, 2015 at 7:51 PM
I've been scouring this thread and have read the documentation and downloaded the sample code. I'm still struggling to populate the font dropdown and color dropdown. It's not a static propertygrid so it's a little complicated. I have a canvas application where a user can drag and drop objects from a toolbox. Once the object is selected, the propertygrid opens up for the object. User should be able to configure it's properties like fonts, color of font etc. I have the object's viewmodel bound to the propertygrid selected object and the simple ones(text boxes) show up fine. I don't know how to show all the different fonts and colors in the dropdown. If anybody has a sample working code I would really appreciate it.
Developer
Aug 31, 2015 at 12:32 PM
Hi,

You can use the ColorPicker as an editor for brushes. Here's an example :
 <Window.Resources>
      <local:MyColorConverter x:Key="MyColorConverter" />
   </Window.Resources>
   
    <Grid>
      <xctk:PropertyGrid x:Name="_propertyGrid">
         <xctk:PropertyGrid.EditorDefinitions>
            <xctk:EditorTemplateDefinition TargetProperties="Background">
               <xctk:EditorTemplateDefinition.EditingTemplate>
                  <DataTemplate>
                     <xctk:ColorPicker SelectedColor="{Binding Value, Converter={StaticResource MyColorConverter}}" />
                  </DataTemplate>
               </xctk:EditorTemplateDefinition.EditingTemplate>
            </xctk:EditorTemplateDefinition>
         </xctk:PropertyGrid.EditorDefinitions>
      </xctk:PropertyGrid>
   </Grid>

 public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      _propertyGrid.SelectedObject = new MyObject()
      {
        Name = "TEST",
        Background = Brushes.Red,
        FontFamily = new FontFamily("Comic Sans MS")
      }; 
    }
  }

  public class MyObject
  {
    public string Name
    {
      get;
      set;
    }

    public Brush Background
    {
      get;
      set;
    }

    public FontFamily FontFamily
    {
      get;
      set;
    }
  }

  public class MyColorConverter : IValueConverter
  {
    public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
    {
      if( value is SolidColorBrush )
      {
        return ((SolidColorBrush)value).Color;
      }
      return null;
    }

    public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
    {
      return new SolidColorBrush( (Color)value );
    }
  }