I have a comboBox that has an ItemsSource of a list of objects. So the DisplayMemberPath is set to a particular property of the object. Of course this means that the correct value is displayed in the ComboBoxItem.
My issue is that I would like to be able to get that "Value" that is returned by the DisplayMemberPath in XAML so that I can bind it to something else. i.e. I would like to have a "DisplayText" property on the ComboBoxItem.
Of course I don't have this, so, does anyone know of a way to get this value without traversing down into the template of the ComboBoxItem looking for the ContentHost?
If you're interested in my specific use of this, I'm trying to do this on the style of the ComboBox:
....
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style>
<Setter
Property="AutomationProperties.AutomationId"
Value="{Binding RelativeSource={RelativeSource Self}, Path=MagicPathForDisplayedText}"/>
....
Of course Path=Content
works just fine if you're just binding your ItemsSource to properties, but when it's an Object with a DisplayMemberPath, Content will be that Object.
Thanks for any help or re-framing of the problem.
The easiest way to handle problems like this is usually Attached Properties and Behaviors.
You could create two attached properties called
DisplayMemberPath
andDisplayText
, then you bindDisplayMemberPath
to the parentComboBox
DisplayMemberPath
and in thePropertyChangedCallback
you set up a binding of your own with the same path forDisplayText
. After that you have a property which you can bind toDisplayTextBehavior