Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

New Post: Sample code to populate fonts and colors in property grid ?

$
0
0
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 );
    }
  }

Viewing all articles
Browse latest Browse all 4964

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>