This project has moved and is read-only. For the latest updates, please go here.

Property Grid with dynamic editors

Oct 29, 2014 at 12:57 PM
I have a property grid which works great at the moment, however for one of the properties I would like the editor to change from an updown counter to a combo box depending on the users selections. Is there any easy way to do this using data triggers?
Developer
Oct 29, 2014 at 1:53 PM
Are you looking for something like this https://wpftoolkit.codeplex.com/workitem/18507 ?

If so, v2.4 should fill your needs.
Nov 4, 2014 at 3:20 PM
That's close but not quite what I want. I want to change the actual type of the editor not just its itemsoure. For example a regular user might have selections constrained by using a combobox, whilst an administrator might have full editing rights and see a textbox.
Developer
Nov 5, 2014 at 1:36 PM
Edited Nov 5, 2014 at 1:36 PM
Hi,

If you have a property like "FirstName", which has its own editor "FirstNameEditor" and that depends on another property like "IsMale" :
public bool IsMale { get; set; }

[Editor( typeof( FirstNameEditor ), typeof( FirstNameEditor ) )]
[DependsOn( "IsMale" )]
public string FirstName { get; set; }
Then you can do what you want in the FirstNameEditor...including modifying the type of the editor :
public class FirstNameEditor : Xceed.Wpf.Toolkit.PropertyGrid.Editors.ITypeEditor
  {
    public FrameworkElement ResolveEditor( Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem )
    {      
      if( ( ( Person )propertyItem.Instance ).IsMale )
      {
        ComboBox comboBox = new ComboBox();
        comboBox.Foreground = new SolidColorBrush( Colors.Green );
        comboBox.ItemsSource = new List<string>() {"Tom", "Mark", "Bryan"};
        comboBox.SelectedIndex = 0;

         var _binding = new Binding( "Value" ); //bind to the Value property of the PropertyItem
        _binding.Source = propertyItem;
        _binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
        BindingOperations.SetBinding( comboBox, ComboBox.SelectedItemProperty, _binding );
        return comboBox;
      }
      else
      {
        TextBox textBox = new TextBox();
        textBox.Foreground = new SolidColorBrush( Colors.Blue );
        textBox.Text = "TEST";
        var _binding = new Binding( "Value" ); //bind to the Value property of the PropertyItem
        _binding.Source = propertyItem;
        _binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
        BindingOperations.SetBinding( textBox, TextBox.TextProperty, _binding );
        return textBox;
      }
    }
  }
So yes, the type of the editor can be changed depending on another property;s value.
This is available in v2.4 Plus version of the Toolkit.
Nov 5, 2014 at 2:56 PM
Is there a xaml equivalent of this code or does it have to be done in the code behind?
Developer
Nov 6, 2014 at 3:35 PM
The ResolveEditor() method is the one that will get called when the editor needs to be re-evaluated. So action has to be done in this method.
I believe you could define your ComboBox/TextBox in XAML and refer to it in code-behind via
var myComboBox = this.Resources["MyComboBox"] as ComboBox;
var myTextBox = this .Resources["MyTextBox"] as TextBox;