如何通过基于XAML的数据绑定访问导航属性?

时间:2016-10-21 20:01:25

标签: c# wpf xaml mvvm data-binding

我正在尝试学习WPF和MVVM,并且正在努力学习一些数据绑定。我有一个包含可观察项目集合的ComboBox,以及一个绑定到ComboBox的SelectedItem并显示EmployeeID值的TextBox。我希望通过导航属性显示与EmployeeID值对应的员工姓名(如果可能),但不知道如何修改XAML以允许这样做。

public class Project
{
    public int ProjectID;
    public string ProjectName;
    public int EmployeeID;
    public Employee Emp;
}

public class Employee
{
    public int EmployeeID;
    public string EmployeeName;        
}

MainView XAML

<ComboBox x:Name="cbxProjects" Width="75" Height="20" 
          ItemsSource="{Binding Projects}"                      
          SelectedValue="{Binding Path=SelectedProject}"
          DisplayMemberPath="ProjectName" />

<TextBox x:Name="tbxEmployee" Height ="20" Width="100" BorderThickness="1"
         Text="{Binding Path=SelectedProject.EmployeeID}"/>

MainViewModel

private ObservableCollection<Project> _projects;
private Project _selectedProject;

public ObservableCollection<Project> Projects
{
    get { { return _projects; } }                                        
}

public Project SelectedProject
{
    get { return _selectedProject; }
    set
    {
        if (_selectedProject != value)
        {
            _selectedProject = value;
            NotifyPropertyChanged("SelectedProject");                    
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您是不是只想绑定到Path=SelectedProject.Emp.EmployeetName