简体   繁体   English

更新ComboBox所选项目

[英]Updating ComboBox selected item

I have a ComboBox that is that is being populated from an ItemsSource collection. 我有一个ComboBox ,它是从ItemsSource集合中填充的。 I am populating all the customers into an Observable Collection, and binding it to the collection, like so. 我将所有客户填充到Observable Collection中,然后将其绑定到集合中,就像这样。

<ComboBox ItemsSource="{Binding Path=Customers}"
          SelectedValue="{Binding CustomerKey}"
          DisplayMemberPath="FullName" SelectedValuePath="{Binding Key}" />

In this same view, I have a data grid with basic information with orders in it. 在同一视图中,我有一个数据网格,其中包含订单的基本信息。 When an order is selected, within the view model I am getting a more detailed record for the order. 选择订单后,在视图模型中,我将获得订单的更详细记录。 What I want to do is update the view with customer information for the newly selected order. 我想要做的是使用新选择的订单的客户信息更新视图。

I was trying to set the CustomerKey , and then selected that Customer based on it's key -- I think I am doing this all wrong though. 我试图设置CustomerKey ,然后根据它的关键选择该客户 - 我认为我这样做是错的。 How can I accomplish this? 我怎么能做到这一点?

The SelectedValuePath property of the ComboBox should be set to a string (and not to a binding) that specifies the name of the property of the Customer class where the key is stored: ComboBox的SelectedValuePath属性应设置为字符串 (而不是绑定),该字符串指定存储密钥的Customer类的属性名称:

<ComboBox ItemsSource="{Binding Path=Customers}"
          SelectedValue="{Binding CustomerKey}"
          DisplayMemberPath="FullName" SelectedValuePath="Key" />

This will work provided that the Customer class has a FullName and a Key property and that the Key property has the same type as the CustomerKey property of the view model. 只要Customer类具有FullName和Key属性并且Key属性与视图模型的CustomerKey属性具有相同的类型,这将起作用。

A customer with a key matching the value that you set the CustomerKey property to must also be present in the Customers collection that you bind the ComboBox to for an item to be selected. 具有与您设置CustomerKey属性的值匹配的键的客户也必须存在于您将ComboBox绑定到要选择的项目的Customers集合中。

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

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