2016-11-23 79 views
0

说我要填写简单的Employee对象列表的组合框...动态更改组合框的DisplayMemberPath?

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查找并按项目选择员工记录。但是,如果员工目前没有与他们关联的项目,我还希望能够按名称或部门查找记录(使用相同的组合框和绑定),如果我愿意的话。关于如何实现将改变DisplayMemberPath值的任何想法,就像我上面描述的那样?

回答

0

DisplayMemberPath只是a string property。在您的视图模型中绑定它。

<ComboBox ... 
    DisplayMemberPath="{Binding DispMemberPath}" 
/> 

视图模型...

private string _DispMemberPath; 
public string DispMemberPath 
{ 
    get {return _DispMemberPath; } 
    set { _DispMemberPath= value; RaiseNotifyPropertyChanged(); } 
} 

private void SetDepartmentAsPath() 
{ 
    this.DispMemberPath = "Department"; 
}