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

Need to set selected item on a custom editor

Aug 21, 2012 at 12:14 AM

We have a custom editor applied to a property via the Editor attribute.

    [Editor(typeof(HorizontalAlignmentEditor), typeof(HorizontalAlignmentEditor))]
    public HorizontalAlignment HorizontalAlignment
    {
        get { return (HorizontalAlignment)GetValue(HorizontalAlignmentProperty); }
        set { SetValue(HorizontalAlignmentProperty, value); }
    }

Basically, the editor is a ListBox with a template to make it display 3 images horizontally instead of a ComboBox with Top, Middle, Bottom or Left, Center, Right displayed when it was dropped. The properties are bound to a Label and it's Horizontal and Vertical alignment properties are bound to the PropertyGrid values.

Everything is working very well, except that when the PropertyGrid is first displayed, the custom editor SelectedItem is not being set, so nothing is highlighted. As soon as the user clicks on the images, they are active and clicking them causes the label properties to be updated.

How can we access the custom editor to set the selected index when the PropertyGrid is loaded?

 

Aug 21, 2012 at 12:55 PM

Issue created based on this discussion:

https://wpftoolkit.codeplex.com/workitem/18515

 

Aug 30, 2012 at 2:19 PM

Here is a work-around which can be used in conjunction with the abve post:

<!-- Editors -->
<xctk:PropertyGrid.EditorDefinitions>
    <xctk:EditorDefinition>
        <xctk:EditorDefinition.PropertiesDefinitions>
            <xctk:PropertyDefinition Name="HorizontalAlignment" />
        </xctk:EditorDefinition.PropertiesDefinitions>
        <xctk:EditorDefinition.EditorTemplate>
            <DataTemplate>
                <extensions:HorizontalAlignmentEditor Value="{Binding Value}" />
            </DataTemplate>
        </xctk:EditorDefinition.EditorTemplate>
    </xctk:EditorDefinition>
</xctk:PropertyGrid.EditorDefinitions>
Developer
Oct 2, 2012 at 3:48 PM

When declaring :

 [Editor(typeof(HorizontalAlignmentEditor), typeof(HorizontalAlignmentEditor))]
    public HorizontalAlignment HorizontalAlignment
    {
        get { return (HorizontalAlignment)GetValue(HorizontalAlignmentProperty); }
        set { SetValue(HorizontalAlignmentProperty, value); }
    }

the below code for the custom editor works under v1.6 and v1.8. When loading the PropertyGrid, the ListBox.SelectedItem of the Custom Editor is highlighted based on the value of PropertyGrid.SelectedObject's HorizontalAlignment property :

public class HorizontalAlignmentEditor : Xceed.Wpf.Toolkit.PropertyGrid.Editors.ITypeEditor
  {
    public FrameworkElement ResolveEditor( Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem )
    {
      ListBox myListbox = new ListBox();
      List<HorizontalAlignment> myList = new List<HorizontalAlignment>();
      myList.Add( HorizontalAlignment.Left);
      myList.Add( HorizontalAlignment.Center);
      myList.Add(HorizontalAlignment.Right);
      myListbox.ItemsSource = myList;

      //create the binding from the bound property item to the editor to highlight the Selected Item
      var _binding = new Binding( "Value" ); //bind to the Value property of the PropertyItem
      _binding.Source = propertyItem;
      _binding.ValidatesOnExceptions = true;
      _binding.ValidatesOnDataErrors = true;
      _binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
      BindingOperations.SetBinding( myListbox, ListBox.SelectedItemProperty, _binding );

      return myListbox;
    }
  }