Populating combobox with dynamic values

Editors for WPF Forum

Posted 11 years ago by Sasha
Version: 13.2.0591
Avatar

Hi,

I have a propertyGrid where I add properties dynamically. This propertyGrid I implement this way:

<propgrid:PropertyGrid IsSummaryVisible="True" AreDefaultSortDescriptionsEnabled="False"  
                            AreCategoriesAutoExpanded="False" SelectedObject="{Binding SelectedObject}"
                            IsAsynchronous="True">
                       <propgrid:PropertyGrid.PropertyEditors>
                            <propgrid:DialogTextBoxPropertyEditor PropertyName="ImagePath"/>
                            <propgrid:ComboBoxPropertyEditor PropertyName="MyCollection" />
                       </propgrid:PropertyGrid.PropertyEditors>
</propgrid:PropertyGrid>

 And properties for this propertyGrid defined :

[LocalizedCategory("Properties"), LocalizedDisplayName("DisplayMyProperty"), LocalizedDescription("DescMyProperty")]
public ObservableCollection<int> MyCollection
{
    get
    {
        _myCollection.Add(2);
        _myCollection.Add(3);
        return _myCollection;
    }
    set
    {
        _myCollection = value;
        RaisePropertyChanged("MyCollection");
                
    }
}
private ObservableCollection<int> _myCollection = new ObservableCollection<int>();
 

 So the problem is that property MyCollection is shown like combobox, but values are not populated. I mean in my property value field I see System.Collections.ObjectModel.ObservableCollection`1[System.Int32], and when I expand this combobox, there are no values.

Where I am wrong or may be there is an example of populating combobox with dynamic properties?

 

Comments (6)

Answer - Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Sasha,

The ComboBox control in general is only meant to return a single value but here you are wiring it up to a collection instead.  So in plain WPF you are effectively trying to do something like ComboBox.SelectedItem = MyCollection, which is an ObservableCollection<int>.

The ComboBoxPropertyEditor's dropdown items are populated by the related IPropertyDataAccessor.StandardValues collection.  StandardValues is generally just populated by property descriptors for enum values when the related target property is an enum, etc.

The only way to edit a collection property would be to use the collection editing features as described in the documenation and collection-related QuickStarts.

If you mean that you want to keep a ComboBox UI but have its drop-down values change on the fly, sorry but we don't have a sample for that.  You might need some sort of custom property editor to do that.  In the custom property editor template, perhaps bind the ComboBox.ItemsSource to some static property somewhere that can change and provides INotifyPropertyChanged notifications so the ComboBox knows to update its items list.


Actipro Software Support

Posted 11 years ago by Sasha
Avatar

No, I am trying to load some values from database, add them to property(something like MyCollection) and then bind this property to propertyGrid to see loaded values in combobox.

So what is the best way to do this?

Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Sasha,

Did you try the suggestion at the end of our last post?


Actipro Software Support

Posted 11 years ago by Sasha
Avatar

Yes, thanks, everything is working. But in the forums i found the following "Once you implement a custom TypeConverter, then you can do away with your custom property editor, as our default property editor knows how to hook into the associated TypeConverter." That's why I am asking about the best way.

Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

There are a couple other ways you could do it, yes.  This post is a good jumping off point that discusses how you could possibly implement your options as "standard values" that would become the items of the ComboBox.  The post links to another thread discussing TypeConverter.


Actipro Software Support

Posted 11 years ago by Sasha
Avatar

Thank you for your help

The latest build of this product (v24.1.3) was released 26 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.