简体   繁体   中英

Binding a datagrid to the selected item of another datagrid

Ive got two DataGrids. EmployeeGrid and WorkSessionsGrid. Each Employee has a list of WorkSessions that I want the user to access by selecting an Item in the EmployeeGrid which should make the WorkSessionsGrid generate the WorkSessions for the selected Employee. Why is the following not correct?

<DataGrid Name="dg_2" ItemsSource="{Binding ElementName=dg_1, Path=SelectedItem.WorkSessions}"/>

Update I've come to the conclusion that the problem has to be in one of the other layers. Here's the remainder of my code, hopefully someone is capable of helping me out. Is there something fundamentally that I am missing?

Code-Behind xaml

    public partial class MainWindow : Window
{
    public EmployeeViewModel EmployeeViewModel = new EmployeeViewModel();


    public MainWindow()
    {
        InitializeComponent();

        menu_employee.DataContext = EmployeeViewModel;
        sp_employee.DataContext = EmployeeViewModel;
        datagrid_employees.ItemsSource = EmployeeViewModel.EmployeesView;
        sp_worksessions.DataContext = EmployeeViewModel.SelectedEmployee.WorkSessions;
        menu_worksession.DataContext = EmployeeViewModel.SelectedEmployee.WorkSessions;
        datagrid_worksessions.ItemsSource = EmployeeViewModel.SelectedEmployee.WorkSessions;

    }
}

WorkSessionViewModel

class WorkSessionViewModel : ViewModelBase
{
    private WorkSessions _workSessionsModel = new WorkSessions();
    public WorkSessions WorkSessionsView = new WorkSessions();

    private WorkSessionModel _selectedWorkSession = new WorkSessionModel();
    public WorkSessionModel SelectedWorkSession
...

WorkSessionModel

[Serializable]
public class WorkSessions : ObservableCollection<WorkSessionModel>
{
    public WorkSessions()
    {

    }
}
[Serializable]
public class WorkSessionModel : INotifyPropertyChanged
{
    private DateTime _dateTime;
    private string _id;
    private double _hours;
    public WorkSessionModel()
    {
    }

改为尝试绑定到元素。

<DataGrid Content="{Binding ElementName=ListOfEmp, Path=SelectedItem.Name}" DataContext="{Binding}" />

This bit of XAML looks quite correct, try to debug the binding , there might be some other problems like visual tree breaks or the WorkSessions collection perchance is a field and not a property etc.

If there are binding errors please share them.

As @HB has pointed out correctly, please use your Visual Studio's Output Window to see any binding errors. They will tell you if the bindings are failing. If you find binding erros then yours binding should be solved for two possible issues...

  1. Source of data is incorrect. Is the data context and items source correctly set for that UI element such as DataGrid ?
  2. Path of the property in the binding could be incorrect. Is your SelectedItem having an object has any property named WorkSessions in it? etc.

Apart from this we still dont know what dg_1 and dg_2 from your XAML is. Your code behind shows different names datagrid_employees and datagrid_worksessions I guess.

You should add one more item for the EmployeeViewModel, called: SelectedEmployee and bind that with the employee grid selected item, mode=TwoWay.

Then you databind for the second grid should be:

<DataGrid Name="dg_2" ItemsSource="{Binding Path=SelectedEmployee.WorkSessions}"/>

Since both grids are in the same window, thus, you should only set datacontext for the windows only. In side the viewmodel, you have 2 dependency properties: EmployeeList, SelectedEmployee. Whereas, EmployeeList is binded to ItemsSource of the employee grid. SelectedEmployee is binded to SelectedItem on the employee grid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM