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

Selecting from a list of options when the property is dynamically generated

4 Answers 272 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 16 Oct 2012, 03:09 PM
I am binding my RadPropertyGrid to an object that contains a "property bag" - in other words, the object has a collection of child objects that represent the properties displayed in the property grid.  This collection is populated from a database so there is no knowledge at all in the application of the names, values, etc. for any of the 'properties'.

The object subclasses CustomTypeDescriptor, overrides GetProperties and returns a custom PropertyDescriptor for each of the items in the collection.  All of this works great except for cases where I need the user to select from a list of options.  Note that the list of options is NOT an enumeration but a collection of name/value pairs obtained from the database.

So the question is how to display a combobox in the property grid that is bound dynamically?

I've seen the other forum posts and examples using the LookupPropertyDefinition but they all use static references in the XAML to the property names and use static resources as the ItemsSource.  Neither of these are possible in my case as I won't know which 'properties' have options until runtime as the list is configurable in the database.  In addition, the list of choices is pulled out of the database so it will be different for each 'property'.

In previous incarnations of our application, we've been able to simply define a custom TypeConverter for the property, override the GetStandardValuesSupported method to return true then override GetStandardValues which would return the list of choices.  As I have learned from other posts, this is not supported in the RadPropertyGrid.  So what is?

Your help is greatly appreciated as this is a significant enough feature and enough time has been spent trying to get it working that it could lead us to another vendor that is capable of supporting this need.

4 Answers, 1 is accepted

Sort by
0
Scott
Top achievements
Rank 2
answered on 17 Oct 2012, 03:05 PM

Update on my Colleague and my issue.


In the Xaml when implementing the following code:

<UserControl
.
.
.
.
xmlns:propertyGrid="clr-namespace:Telerik.Windows.Controls.Data.PropertyGrid;assembly=Telerik.Windows.Controls.Data"
.
.
.
>
 
<UserControl.Resources>
            <local:PropertyGridDataTemplateSelector.OtherDataTemplate>
                <DataTemplate DataType="settings:SettingOptionInfoCollection">
                    <ComboBox
                              propertyGrid:AutoBindBehavior.UpdateBindingOnElementLoaded="ItemsSource"
                              SelectedValue="{Binding Path=SelectedOption, Mode=TwoWay}"
                              />
                </DataTemplate>
            </local:PropertyGridDataTemplateSelector.OtherDataTemplate>
        </local:PropertyGridDataTemplateSelector>
    </UserControl.Resources>
     
    <Grid>
        <telerik:RadBusyIndicator IsBusy="{Binding IsBusy}" >
            <telerik:RadPropertyGrid Item="{Binding Items}" EditorTemplateSelector="{StaticResource dataTemplateSelector}" />
        </telerik:RadBusyIndicator>
    </Grid>
</UserControl>

We are binding to a Property bag object that subclasses CustomTypeDescriptor, overrides GetProperties and returns a custom PropertyDescriptor for each of the items in the collection.  When we have a combo box we return a custom collection of options which populates the combo box items, but when it comes to using the SelectedOption of that collection to bind to the Selected Value of the Combo Box.

Example of the collection:

public class SettingOptionInfoCollection : ObservableCollection<SettingOptionInfo>
{
    private SettingOptionInfo _selectedOption;
 
    public SettingOptionInfoCollection()
    {
    }
 
    public SettingOptionInfo SelectedOption
    {
        get
        {
            return _selectedOption;
        }
        set
        {
            _selectedOption = value;
            OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("SelectedOption"));
        }
    }

The problem is that when the ItemSource is set to:

propertyGrid:AutoBindBehavior.UpdateBindingOnElementLoaded="ItemsSource"

We assumed that the datacontext would set to the binding property of the itemsource being or collection.  Instead it seems that the datacontext is forced set to our "property bag" object that does not have a property called SelectedOptions.  The datacontext of the combobox at this point needs to be that of our itemsource property, from the PropertyGrid, can you help provide some insight on how we can access that binding item from the BindingSource to set our selected item?

Everything works great except for binding the SelectedOption to the SelectedValue property.  Any help would be greatly appreciated.

Current the Binding Path expectes the SelectedOption to be a part of our "Property Bag" Object.

Let me know if you have any questions or don't understand what we are doing.

Thanks,
-Scott

0
Ivan Ivanov
Telerik team
answered on 19 Oct 2012, 02:52 PM
Hi Scott,

Thank you for providing us those clarifications. We will need a bit more time to mimic this behavior on our side. I will contact you back on Monday to share the results of our research.

Greetings,
Ivan Ivanov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Scott
Top achievements
Rank 2
answered on 22 Oct 2012, 12:08 PM
Thanks for getting back Ivan.

I have uploaded an example program that demonstrates our issue to the support ticket system.  The support ticket ID for this issue is 620579.

Please let me know that you got the zipped program and what you find out.

Thanks,
-Scott
0
Ivan Ivanov
Telerik team
answered on 22 Oct 2012, 04:13 PM
Hi Scott,

Thank you for the attached project. Unfortunately the behavior that you are currently experiencing is actually the default one for RadPropertyGrid. As RadPropertyGrid serves as a property editor and visualizer for CLR objects, scenarios when a single option should be chosen as a value of a certain property are quite common. So that, it seems natural that a ComboBox's SelectedValue should be bound to the respective item's property. On the other hand the set of possible options does not really belong to the BusinessObject's domain. Another reason not to set a ComboBox's DataContext to be the ItemsSource would be the need to create custom collection types that expose additional properties (like in your scenario). Personally, I would encourage you to change your architecture a bit and move the SelectedOption property to your BusinessObject and optionally, the OptionsCollection property - to the ViewModel.

Greetings,
Ivan Ivanov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
PropertyGrid
Asked by
James
Top achievements
Rank 1
Answers by
Scott
Top achievements
Rank 2
Ivan Ivanov
Telerik team
Share this question
or