This is a migrated thread and some comments may be shown as answers.

Editor Attribute does not work for nested definition

1 Answer 87 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
Veteran
John asked on 11 Jun 2020, 09:26 PM

Hello,

Using a WPF PropertyGrid. and having a problem with using Editor Attributes in combination with a nested property.  Basically, all property definitions with Editor Attributes defined at the root will work fine, however nested property definitions with Editor Attributes renders with nothing in it.  The attached file shows the problem.

Here is the code which generates the property definitions:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    PropertyDefinitionCollection properties = EditorPropertyGrid.PropertyDefinitions;
 
    ObservableCollection<object> testList = new ObservableCollection<object>();
 
    UserDefinedEnumKeyValue first = new UserDefinedEnumKeyValue() { Key = "Custom Enum Control Prop", Value = new UserDefinedEnum() { Item = "UD3", Items = { "UD1", "UD2", "UD3", "UD4" } } };
    testList.Add(first);
    properties.Add(new PropertyDefinition()
    {
        DisplayName = first.Key,
        Binding = new Binding("Value") { Source = first }
    });
 
    ParentKeyValue item = new ParentKeyValue() { ChildList = new List<KeyValueBase>() };
    testList.Add(item);
    PropertyDefinition parentPropertyDefinition = new PropertyDefinition()
    {
        DisplayName = item.Key,
        Binding = new Binding("Value") { Source = item }
    };
    properties.Add(parentPropertyDefinition);
    PropertyDefinitionCollection nestedProperties = parentPropertyDefinition.NestedProperties;
 
    UserDefinedEnumKeyValue second = new UserDefinedEnumKeyValue() { Key = "Custom Enum Control Prop nested", Value = new UserDefinedEnum() { Item = "UD3", Items = { "UD1", "UD2", "UD3", "UD4" } } };
    testList.Add(second);
    nestedProperties.Add(new PropertyDefinition()
    {
        DisplayName = second.Key,
        Binding = new Binding("Value") { Source = second }
    });
 
    EditorPropertyGrid.Item = testList;
}

 

Here are the model classes:

abstract class KeyValueBase
{
    public string Key { get; set; }
    public abstract void SetValue(string value);
 
}
 
class UserDefinedEnum : INotifyPropertyChanged
{
    private List<string> _items = new List<string>();
    public List<string> Items
    {
        get
        {
            return this._items;
        }
        //set NOTE not used for one way binding
        //{
        //    if (this._items != value)
        //    {
        //        this._items = value;
        //        this.OnPropertyChanged("Items");
        //    }
        //}
    }
 
    private string _item;
    public string Item
    {
        get
        {
            return this._item;
        }
        set
        {
            if (this._item != value)
            {
                this._item = value;
                this.OnPropertyChanged("Item");
            }
        }
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    private void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
 
class UserDefinedEnumKeyValue : KeyValueBase
{
    [Telerik.Windows.Controls.Data.PropertyGrid.Editor(typeof(EnumControl), Telerik.Windows.Controls.Data.PropertyGrid.EditorStyle.None)]
    public UserDefinedEnum Value { get; set; }
    public override void SetValue(string value)
    {
 
    }
}
 
class ParentKeyValue : KeyValueBase
{
    public string Value { get; set; }
    public List<KeyValueBase> ChildList { get; set; }
    public override void SetValue(string value)
    {
 
    }
}

 

And here is the XAML for the EnumControl:

<UserControl x:Class="TelerikWpfApp002.EnumControl"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
    <Grid>
        <telerik:RadComboBox SelectedValue="{Binding Item, Mode=TwoWay}" ItemsSource="{Binding Items, Mode=OneWay}" />
    </Grid>
</UserControl>

 

Thank you in advance for any help offered...

1 Answer, 1 is accepted

Sort by
0
Vladimir Stoyanov
Telerik team
answered on 16 Jun 2020, 12:51 PM

Hello John,

Thank you for the shared picture. 

In order to reuse the UserControl as the editor for nested property definitions, you can make sure to set its DataContext in that scenario.

I am attaching a sample project based on the shared code demonstrating a possible approach for doing so in the Loaded event of the RadComboBox. Please, give this a try and let me know how it goes. 

Regards,
Vladimir Stoyanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
PropertyGrid
Asked by
John
Top achievements
Rank 1
Veteran
Answers by
Vladimir Stoyanov
Telerik team
Share this question
or