繁体   English   中英

在网格中绑定组合框selectedValue

[英]Binding a combobox selectedvalue in Grid

我有一个组合框作为gridview中的一列,它绑定到本地属性,如下所示:

<ComboBox Name="cboMetaDataTypes" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=MetaDataTypes}" 
DisplayMemberPath="Value" SelectedValuePath="Key"/>

Source MetaDataTypes是一个KeyValuePair列表,其中显示Value属性,如您所见。

现在我被困住了。 我想将选定的值绑定到与列的键匹配的键。 我希望将所选值绑定到本地属性。 我做了很多尝试,但我无法实现。 谁能给我指示?

谢谢。

如果我了解所需的内容,则想将SelectedValue属性绑定到Window类中的一个属性(如MyWindow.xaml.cs文件中所定义)。 根据我的经验,最好的方法是为窗口类分配一个名称,如下所示:

<Window x:Name="myWindow" ... >

然后,您可以使用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 }" />

您可能需要将myProperty定义为依赖项属性,以使两种方式的数据绑定都能正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM