I have a combobox as a column in a gridview and it is bound to a local property like so:
<ComboBox Na开发者_C百科me="cboMetaDataTypes" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=MetaDataTypes}"
DisplayMemberPath="Value" SelectedValuePath="Key"/>
The Source MetaDataTypes is a List of KeyValuePairs where the Value property is displayed as you can see.
Now I am stuck. I want to bind the selected value to the Key that matches the Key of the Column. And I want the selected value to be bound to a local property. I tried a lot but I cannot make it happen. Can anyone give me directions?
Thank You.
If I'm understanding what you want, you want to bind the SelectedValue
property to a property in your Window's class (as defined in the MyWindow.xaml.cs file). The best way to do that, in my experience, is to assign your window class a Name like this:
<Window x:Name="myWindow" ... >
And then you can do the following with your ComboBox
<ComboBox Name="cboMetaDataTypes" ItemsSource="{Binding RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type Window}}, Path=MetaDataTypes}"
DisplayMemberPath="Value" SelectedValuePath="Key"
SelectedValue="{Binding ElementName=myWindow, Path=myProperty }" />
You may need to define your myProperty
as a dependency property to get two way data-binding to work properly.
精彩评论