Search code examples
c#wpfxamlpropertygridxceed

Xceed WPF propertyGrid Custom Collection editor


I am new to WPF, and I am using xceed property grid, in my class i have a custom collection editor. As per xceed, I found that I have to implement "Xceed.Wpf.Toolkit.PropertyGrid.Editors.ITypeEditor", I have implemented it, but I get error when trying to set binding. Error message: 'Two-way binding requires Path or XPath'. Below is the c# class, where property is defined:

 /// <summary>
/// The expected assembly versions.
/// </summary>
[DescriptionAttribute("The expected assembly versions.")]
[Category("Mandatory")]
[ExpandableObject]
[Editor(typeof(Assembly), typeof(Assembly))]
public Assembly[] ExpectedAssemblyVersions
{
  get;
  set;
}
   /// <summary>
/// To verify assembly and there versions
/// </summary>
[TypeConverterAttribute(typeof(ExpandableObjectConverter))]
public class Assembly : Xceed.Wpf.Toolkit.PropertyGrid.Editors.ITypeEditor
{
  /// <summary>
  /// Assembly Name
  /// </summary>
  public string Name { get; set; }
  /// <summary>
  /// Assembly versions
  /// </summary>
  public string[] Versions { get; set; }

  #region Implementation of ITypeEditor
  /// <summary>
  /// 
  /// </summary>
  /// <param name="propertyItem"></param>
  /// <returns></returns>
  public FrameworkElement ResolveEditor(PropertyItem propertyItem)
  {
    TextBox textBox = new TextBox();
    var _binding = new Binding(Name);
    _binding.Source = propertyItem;
    _binding.ValidatesOnExceptions = true;
    _binding.ValidatesOnDataErrors = true;
    _binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
    BindingOperations.SetBinding(textBox, TextBox.TextProperty, _binding);
    return textBox;
  }
  #endregion
}

Xaml binding:

<xctk:PropertyGrid Name="pk" Foreground="{Binding SelectedTheme.FontShade}" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" FontWeight="ExtraBold"  Background="{Binding SelectedTheme.DarkShade}" SelectedObject="{Binding SelectedElement,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

the implementation of 'ResolveEditor' is not complete, I need help in fixing this.


Solution

  • Do you mean something like this:

    class Properties
    {
        private List<Assembly> assemblies = new List<Assembly>();
    
        [DisplayName("ExpectedAssemblyVersions")]
        [Description("The expected assembly versions.")]
        [Category("Mandatory")]
        [Editor(typeof(CollectionEditor), typeof(CollectionEditor))]
        public List<Assembly> Assemblies
        {
            get
            {
                return assemblies;
            }
            set
            {
                assemblies = value;
            }
        }
    }
    
    class Assembly
    {
        public string Name { get; set; }
    
        public string Version { get; set; }
    }