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

UserControl as custom editor

Mar 14, 2014 at 3:46 PM
We are trying to use a UserControl as a custom editor in the PropertyGrid. But it is displaying as a TextBox instead of the ComboBox as defined in the UserControl. The UserControl implements the ITypeEditor interface as follows where GroupName is the property that is being updated:
  #region ITypeEditor Members

  // Used to support extended tool kit property grid used in EditLabelPrinter screen.
  public FrameworkElement ResolveEditor(Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem)
  {
      Binding binding = new Binding("GroupName");
      binding.Source = propertyItem;
      binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
      BindingOperations.SetBinding(this, GroupNameUserControl.GroupNameProperty, binding);

      return this;
  }

  #endregion
Below is the xaml declaration.
  <xctk:EditorDefinition>
      <xctk:EditorDefinition.PropertiesDefinitions>
          <xctk:PropertyDefinition Name="GroupName" />
      </xctk:EditorDefinition.PropertiesDefinitions>
      <xctk:EditorDefinition.EditorTemplate>
          <DataTemplate>
              <GroupName:GroupNameUserControl GroupName="{Binding Value}" EditingType="LabelDefinition" CurrentInstance="{Binding RelativeSource={RelativeSource AncestorType={x:Type mwf:ContentScreen}}, Path=DataContext}" ControlGap="3" ToolTip="Can be used to group etchers together"/>
          </DataTemplate>
      </xctk:EditorDefinition.EditorTemplate>
  </xctk:EditorDefinition>
  1. None of the c# code, such as the constructor, is being called.
  2. When we use this UserControl elsewhere, the CurrentInstance is bound to the DataContext.
    I am not sure what to bind the CurrentInstance to in the context of a PropertyGrid.
  3. We define a string type editor later in the xaml declaration - could that be cancelling the
    UserControl editor definition - the type string editor definition is as below:
  <!-- Makes all strings selected when the user tabs through the property grid -->
  <xctk:EditorDefinition TargetType="{x:Type sys:String}">
      <xctk:EditorDefinition.EditorTemplate>
          <DataTemplate>
              <xctk:WatermarkTextBox Text="{Binding Value}" SelectAllOnGotFocus="True"/>
          </DataTemplate>
      </xctk:EditorDefinition.EditorTemplate>
  </xctk:EditorDefinition>
We are using an older version of the extended tool kit, but I cannot change that.

Thanks in advance for any ideas or suggestions!
Developer
Mar 17, 2014 at 7:41 PM
Hi,

Usually, with the current version of the toolkit, we are using the comboBox (as an editor for a propertyItem) this way :
``` <Window.Resources>
  <x:Array x:Key="comboSource" Type="s:String">
     <s:String>String Value 1</s:String>
     <s:String>String Value 2</s:String>
     <s:String>String Value 3</s:String>
  </x:Array>      
</Window.Resources>
<Grid>
  <xctk:PropertyGrid x:Name="_propertyGrid">
     <xctk:PropertyGrid.EditorDefinitions>
        <xctk:EditorTemplateDefinition TargetProperties="GroupName">
           <xctk:EditorTemplateDefinition.EditingTemplate>
              <DataTemplate>
                 <ComboBox ItemsSource="{StaticResource comboSource}"  SelectedItem="{Binding Value}"/>
              </DataTemplate>
           </xctk:EditorTemplateDefinition.EditingTemplate>
        </xctk:EditorTemplateDefinition>

        <xctk:EditorTemplateDefinition TargetProperties="{x:Type s:String}">
           <xctk:EditorTemplateDefinition.EditingTemplate>
              <DataTemplate>
                 <xctk:WatermarkTextBox Text="{Binding Value}"  SelectAllOnGotFocus="True" />
              </DataTemplate>
           </xctk:EditorTemplateDefinition.EditingTemplate>
        </xctk:EditorTemplateDefinition>

     </xctk:PropertyGrid.EditorDefinitions>
  </xctk:PropertyGrid>
</Grid>

public partial class MainWindow : Window
{
public MainWindow()
{
  Xceed.Wpf.Toolkit.Licenser.LicenseKey = "XXXXX-XXXXX-XXXXX-XXXX";
  InitializeComponent();
  _propertyGrid.SelectedObject = new MyData()
  {
    ID = 12,
    FirstName = "MyFirstName",
    LastName = "MyLastName",
    GroupName = ((string[])this.Resources["comboSource"])[1]
  };
}
}

public class MyData : DependencyObject
{
public int ID  {  get;  set; }

public string FirstName  { get; set; }

public string LastName { get; set; }

public string GroupName  {  get; set;  }
}

Setting an Editor for "stings" after having set an editor for a PropertyItem of type "string" shouldn't cancel the first editor.
Are you trying to do something like that ?