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

New Post: PropertyGrid + Custom Editors + ClassLibrary

$
0
0
If you create a Custom editor for specific properties of the selectedObject, the propertyGrid should use the custom editor for these properties.

Here's something that works :
//In the class library
namespace ClassLibrary1
{
    public class Class1
    {
      [Editor( typeof( FirstNameEditor ), typeof( FirstNameEditor ) )]
      public string FirstName
      {
        get;
        set;
      }

      public string LastName
      {
        get;
        set;
      }
    }

    public class FirstNameEditor : Xceed.Wpf.Toolkit.PropertyGrid.Editors.ITypeEditor
    {
      public FrameworkElement ResolveEditor( Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem )
      {
        ComboBox comboBox = new ComboBox();
        comboBox.Foreground = new SolidColorBrush( Colors.Green );
        comboBox.ItemsSource = new List<string>() { "Tom", "Mark", "Bryan" };       
        comboBox.SelectedIndex = 0;

        //create the binding from the bound property item to the editor
        var _binding = new Binding( "Value" ); //bind to the Value property of the PropertyItem
        _binding.Source = propertyItem;
        BindingOperations.SetBinding( comboBox, ComboBox.SelectedItemProperty, _binding );
        return comboBox;
      }
    }
}

//In the Main Application :
 <Grid>
      <xctk:PropertyGrid x:Name="_propertyGrid" />
   </Grid>

 public partial class MainWindow : Window
  {
    public MainWindow()
    {
      Xceed.Wpf.Toolkit.Licenser.LicenseKey = "XXXXX-XXXXX-XXXXX-XXXX";

      InitializeComponent();    

      _propertyGrid.SelectedObject = new ClassLibrary1.Class1() { FirstName = "Tom", LastName = "Thompson"};
    }
  }

Viewing all articles
Browse latest Browse all 4964

Trending Articles



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