PropertyGrid with ItemsSource binding

Grids for WPF Forum

Posted 10 years ago by David Sherwood
Avatar
I wonder if there is a way to handle dynamic properties by binding to ItemSource rather than rebuilding the properties collection.
I have a database of properties. Each row represents a property with one column the name and one column the (string) value.
I created my own class inheriting from PropertyGridPropertyItem where I override the Value property to update my data row. And then I rebuild the properties collection every time the database property list changes.
I would rather bind a collection to ItemsSource and let binding process handle the dynamic changes. Can this be done? The documentation says you can bind ItemsSource to any collection but doesn't say how.

[Modified at 03/15/2010 07:24 PM]

Comments (3)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi David,

If you have an observable collection of PropertyGridPropertyItems, then you can bind that to the PropertyGrid.ItemsSource. Really the objects in the collection just need to implement IPropertyDataAccessor (or ICategoryDataAccessor/ICategoryEditorDataAccessor), which PropertyGridPropertyItem does. You could bind directly to objects that don't implement these interfaces, but it would require a bit more work to get it displaying correctly (as the UI is built to work off of these interfaces).

Binding the ItemsSource to the collection works the same as any other ItemsControl.


Actipro Software Support

Posted 10 years ago by David Sherwood
Avatar
Thanks. I was using List(of) before. But there is one difference: I get no Categories. I have IsCategorized="True" and my PropertyGridPropertyItem sets the Category property. This is not an essential requirement since I only have one category. Would I have to create a PropertyGridCategoryItem and then add by properties to it?
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi David,

That's correct, if you use PropertyGrid.Items or ItemsSource, then you can't use several of the built-in features. When using SelectedObject(s)/Properties, the PropertyGrid uses the current DataFactory to generate a collection, which it assigns to ItemsSource. Things like categorization, filtering (using PropertyGrid.DataFilter), category editors (using PropertyGrid.CategoryEditors), and sorting (using PropertyGrid.SortDescriptions or PropertyGrid.SortComparer) are bypassed when you using Items/ItemsSource directly.

You can certainly perform your own categorization though. Your list would need to support objects of type IDataAccessor, so if you are using generics it would need to be List<IDataAccessor>. You can then explicitly add a PropertyGridCategoryItem to your list, which in turn would have the properties it contains in Accessors. You can add category editors to Accesors also, if needed.

Basically, if you use Items/ItemsSource then you need to build the top-level structure directly. Properties will still be expandable based on the PropertyGrid.PropertyExpandability/TypeConverter and they will pick up the property editors the same way.


Actipro Software Support

The latest build of this product (v2019.1 build 0682) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.