簡體   English   中英

更新ComboBox所選項目

[英]Updating ComboBox selected item

我有一個ComboBox ,它是從ItemsSource集合中填充的。 我將所有客戶填充到Observable Collection中,然后將其綁定到集合中,就像這樣。

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

在同一視圖中,我有一個數據網格,其中包含訂單的基本信息。 選擇訂單后,在視圖模型中,我將獲得訂單的更詳細記錄。 我想要做的是使用新選擇的訂單的客戶信息更新視圖。

我試圖設置CustomerKey ,然后根據它的關鍵選擇該客戶 - 我認為我這樣做是錯的。 我怎么能做到這一點?

ComboBox的SelectedValuePath屬性應設置為字符串 (而不是綁定),該字符串指定存儲密鑰的Customer類的屬性名稱:

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

只要Customer類具有FullName和Key屬性並且Key屬性與視圖模型的CustomerKey屬性具有相同的類型,這將起作用。

具有與您設置CustomerKey屬性的值匹配的鍵的客戶也必須存在於您將ComboBox綁定到要選擇的項目的Customers集合中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM