![](/img/trans.png)
[英]ComboBox SelectedItem = null with DisplayMemberPath
[英]Dynamically change the DisplayMemberPath of a ComboBox?
假設我想用簡單的Employee對象列表填充ComboBox ...
public class Employee
{
private int ID { get; set; }
public string Name { get; set; }
public string Department { get; set; }
public string Project { get; set; }
}
然后像這樣通過XAML綁定該列表...
<ComboBox ItemsSource="{Binding EmployeeList}"
SelectedValue="{Binding SelectedEmployee}"
DisplayMemberPath="Project"/>
現在,我可以使用此ComboBox查找並按Project選擇Employee記錄。 但是,如果某個雇員當前沒有與之關聯的項目,我也希望能夠按我選擇的名稱或部門查找記錄(使用相同的ComboBox和綁定)。 關於如何實現將更改如上所述的DisplayMemberPath值的任何想法?
DisplayMemberPath只是一個字符串屬性 。 綁定到您的視圖模型中。
<ComboBox ...
DisplayMemberPath="{Binding DispMemberPath}"
/>
視圖模型...
private string _DispMemberPath;
public string DispMemberPath
{
get {return _DispMemberPath; }
set { _DispMemberPath= value; RaiseNotifyPropertyChanged(); }
}
private void SetDepartmentAsPath()
{
this.DispMemberPath = "Department";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.