Binding editor to multiple properties

Grids for WPF Forum

Posted 8 years ago by Mick
Version: 11.1.0541
Avatar
I have an object that has a Category and SubCategory property. I want the SubCategory options change depending on what is set for the main category. I also want to display the options to the user in combobox form.

Example Data:

Category UPPERCASE:
SubCategory: VALUE1
SubCategory: VALUE2

Category lowercase:
SubCategory: value1
SubCategory: value2

I have each of the properties displaying to the user via combo boxes (I create DataTemplates for the properties and assign the "[Editor(...)]" attribute to the property.

What I want is to be able to change the ItemsSource of the "SubCategory" combo box based on the value in the "Category" combo box. In the example data, when they select "lowercase", "value1" and "value2" will show up while "VALUE1" and "VALUE2" will not.

How do I correctly do this?

---

Here is the code that I am using for the DataTemplate:

<DataTemplate x:Key="MyTemplate">
 <Grid>
  <ComboBox
    SelectedValue=
    "{Binding Value,
    RelativeSource={RelativeSource AncestorType={x:Type Primitives:IPropertyDataAccessor}},
    Mode=TwoWay,
    ValidatesOnExceptions=True,
    NotifyOnValidationError=True}"
    ItemsSource=???
    />
 </Grid>
</DataTemplate>

Comments (5)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Mick,

You can access the underlying object using something like "{Binding DataContext.Target, RelativeSource={RelativeSource AncestorType={x:Type propgrid:PropertyGridDataAccessorItem}}}". So if the object has the list of valid values, then you can bind to it like "{Binding DataContext.Target.SubValues...".

Otherwise, you could bind ItemsSource to the Category property and use a custom IValueConverter to return one list of values or other.


Actipro Software Support

Posted 8 years ago by Mick
Avatar
Thank you,

The object that I am trying to bind to does not have a list of valid values. It is a "dumb" object that only knows 2 string fields - "category" and "subcategory." The list of valid values will get populated by a service call that my BO has no knowledge of.

When I try to bind the ItemsSource of my ComboBox, I get the following error:

InvalidOperationException : A TwoWay or OneWayToSource binding cannot work on the read-only property 'Target' of type 'ActiproSoftware.Windows.Controls.PropertyGrid.Primitives.PropertyDescriptorDataAccessor'.

A OneWay binding works just fine, but causes the problem that when I update the "Category" (main) property, it does update the "SubCategory" property when changed.

Because I want the source for the "SubCategory" box to change based on the "Category" box, a two way binding seems necessary.

How do I get around this?
Posted 8 years ago by Mick
Avatar
After reading my post, I realize that I may have opened the door to you offering a solution using the binding:

"{Binding DataContext.Target.Category" (this will presumably not throw the error above, because "Category" is not a readonly property)

to get the "SubCategory" ItemsSource to refresh.

However, the "SubCategory" dropdown might change based on the state of the entire object, not just the "Category" property, so I'm not sure, but I don't believe this will be a viable solution.

Thank you,
Mick
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Mick,

I'm not entirely sure what you are trying to do. You would need to bind the ItemsSource to a list of potential values, but there are several ways to do this. You do not want this to be a TwoWay binding or a OneWayToSource, as you should define the binding on the ItemsSource property and do not what it's changes passed back.

You would either need to expose the potential values on your object or use a MultiBinding and a custom IMutliValueConverter. In the former case, your combine all the values that determine the potential values in your object. In the latter case the IMultiValueConverter does it, but you'd need to bind to ever property that determines the potential list of values.


Actipro Software Support

Posted 8 years ago by Mick
Avatar
Hello,

Thank you for your quick response.

Mick

[Modified at 02/28/2011 04:51 PM]
The latest build of this product (v2018.1 build 0675) was released 7 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.