PropertyGrid: Binding of List<CustomClass>

Sep 12, 2012 at 9:17 AM
Edited Sep 12, 2012 at 9:21 AM

ffffHello,

I'd like to display a list of objects that are of CustomClass in my PropertyGrid.
In my CustomClass there is a Property called Name.
Now I want to display the "Name" of each object in a ListBox:

<ListBox x:Name="lstBox" DisplayMemberPath="Name" ItemsSource="{Binding Path=MyList}" />

The Editor looks like this:

public static readonly DependencyProperty MyListProperty =
            DependencyProperty.Register(
            "MyList",
            typeof(List<CustomClass>),
            typeof(MyEditor),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public List<CustomClass> MyList
{
            get { return (List<CustomClass>)GetValue(SignalsProperty); }
            set { SetValue(MyListProperty, value); }
}

public FrameworkElement ResolveEditor(PropertyItem propertyItem)
{
            Binding binding = new Binding("MyList");
            binding.Source = propertyItem;

            binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
            BindingOperations.SetBinding(this, MyEditor.MyListProperty, binding);	   return this;
When "SetBinding" is executed, MyList is still null and my items are not displayed. What am I missing?
Sep 12, 2012 at 12:03 PM

Your "MyList" property is defined on your "this" instance and you assign the source of your binding to "propertyItem".

try:

binding.Source = this;

Sep 13, 2012 at 8:43 AM
Edited Sep 13, 2012 at 8:54 AM

It worked perfectly when I was binding to a string:

public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(MyEditor), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public string Value { get { return (string)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } }
public FrameworkElement ResolveEditor(Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem)
{
            Binding binding = new Binding("Value");
            binding.Source = propertyItem;
            binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
            BindingOperations.SetBinding(this, MyEditor.ValueProperty, binding);


            return this;
}

When debugging, my Property "Value" is assigned right after the SetBinding operation. When trying to bind to List<CustomClass> its value stays null...

Thank you for your help

EDIT: After doing everything the way as in the string-example, it works... I am confused

Sep 14, 2012 at 4:53 PM

The reason why it work with a string, is that you "List<CustomClass>" is a reference type, and a string (behave like) a value type.

Probably that your List end up being used by more than one control at a time, wich cause your problem.