我有一个 ComboBox
即从 ItemsSource
填充收藏。我将所有客户填充到一个可观察的集合中,并将其绑定(bind)到集合,就像这样。
<ComboBox ItemsSource="{Binding Path=Customers}"
SelectedValue="{Binding CustomerKey}"
DisplayMemberPath="FullName" SelectedValuePath="{Binding Key}" />
在同一个 View 中,我有一个数据网格,其中包含基本信息和订单。选择订单后,在 View 模型中,我将获得更详细的订单记录。我想要做的是使用新选择的订单的客户信息更新 View 。
我试图设置
CustomerKey
,然后根据它的键选择该客户——不过我认为我做错了。我怎样才能做到这一点?
最佳答案
ComboBox 的 SelectedValuePath 属性应设置为字符串(而不是绑定(bind)),该字符串指定存储 key 的 Customer 类的属性名称:
<ComboBox ItemsSource="{Binding Path=Customers}"
SelectedValue="{Binding CustomerKey}"
DisplayMemberPath="FullName" SelectedValuePath="Key" />
如果 Customer 类具有 FullName 和 Key 属性,并且 Key 属性具有与 View 模型的 CustomerKey 属性相同的类型,这将起作用。
具有与您将 CustomerKey 属性设置为的值匹配的键的客户也必须存在于您将 ComboBox 绑定(bind)到的客户集合中,才能选择项目。
关于c# - 更新 ComboBox 选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41730889/