與wpf ComboBox DisplayMemberPath、SelectedValue和SelectedValuePath混淆

[英]Confused with wpf ComboBox DisplayMemberPath,SelectedValue and SelectedValuePath


I have always struggled with those comboBox properties

我總是糾結於那些組合框屬性

  1. DisplayMemberPath
  2. DisplayMemberPath
  3. SelectedValue
  4. SelectedValue
  5. SelectedValuePath
  6. SelectedValuePath

I am building a master detail form .

我正在構建一個master detail表單。

  1. ComboBox filled with Customers
  2. 組合框擠滿了顧客
  3. User Selects a Customer in Combo
  4. 用戶在組合中選擇一個客戶。
  5. All the textBoxes EG Fills correctly
  6. 所有的文本框都填對了

The problem I am having I have made it work but I dont understand those properties and the differences. Is there a noddy example explaining what they do ? You might think is self explanatory.Well not to me.

我遇到的問題是,我讓它工作了,但是我不理解這些性質和區別。是否有一個愚蠢的例子來解釋他們的行為?你可能認為這是不言自明的。不是給我。

Thanks for your input

謝謝你的輸入

1 个解决方案

#1


136  

I think we can understand this better with an example. See this class:

我想我們可以通過一個例子來更好地理解這個問題。看到這個類:

public class Employee
{
   public int Id { get; set; }
   public string Name { get; set; }
}

and the following xaml:

和下面的xaml:

<ComboBox ItemsSource="{Binding Source={StaticResource Employees}}"
          DisplayMemberPath="Name"
          SelectedValuePath="Id"/>

DisplayMemberPath points to the Name property, so the value displayed in the ComboBox and the Employee entries contained in the drop down list, will be the Name property of the Employee object.

DisplayMemberPath指向Name屬性,因此在ComboBox中顯示的值和包含在下拉列表中的Employee條目將是Employee對象的Name屬性。

To understand the other two, you should first understand SelectedItem. SelectedItem will return the currently selected Employee object from the ComboBox. You can also assign SelectedItem with an Employee object to set the current selection in the ComboBox.

要理解另外兩個,您應該首先理解SelectedItem。SelectedItem將從ComboBox返回當前選中的Employee對象。您還可以使用Employee對象分配SelectedItem來設置ComboBox中的當前選擇。

SelectedValuePath points to Id, which means you can get the Id of currently selected Employee by using SelectedValue. You can also set the currently selected Employee in the ComboBox by setting the SelectedValue to an Id (which we assume will be present in the Employees list).

SelectedValuePath指向Id,這意味着您可以使用SelectedValue獲取當前所選員工的Id。您還可以通過將SelectedValue設置為Id(我們假設它將出現在Employees列表中)來設置ComboBox中當前選中的Employee。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/09/26/7298b399b19a824b476c7f767a86e2c.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com