Reputation: 6666
Once again I ask you, stackoverflow, in my time of need.
I am trying to bind a Toolbar, a Menu and a DataGrid(datagrid_worksessions) to the selected item of another DataGrid(datagrid_employees). The tricky part is that I need the Toolbar and Menu to bind to the elements ViewModel layer while the DataGrid binds to the Model layer of that specific object.
This is the Code-Behind of my ViewLayer. Let me know if you need more than this.
namespace SalaryApplication
{
public partial class MainWindow : Window
{
public EmployeeViewModel EmployeeViewModel = new EmployeeViewModel();
private ObservableCollection<WorkSessionModel> SelectedWorkSessions = new ObservableCollection<WorkSessionModel>();
public MainWindow()
{
InitializeComponent();
menu_employee.DataContext = EmployeeViewModel;
sp_employee.DataContext = EmployeeViewModel;
datagrid_employees.ItemsSource = EmployeeViewModel.EmployeesView;
sp_worksessions.DataContext = EmployeeViewModel.WorkSessionViewModel;
menu_worksession.DataContext = EmployeeViewModel.WorkSessionViewModel;
}
private void datagrid_employees_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
grid_selectedEmployee.DataContext = EmployeeViewModel.SelectedEmployee;
SelectedWorkSessions = EmployeeViewModel.SelectedEmployee.WorkSessions;
datagrid_worksessions.ItemsSource = SelectedWorkSessions;
}
}
}
Upvotes: 0
Views: 1191
Reputation: 19895
DataContext is a inheritable Dependency property which means if you set it for the parent GUI, then all the children elements will automatically acquire it.
So in your case just setting the data context of the window \ user control will make menu_employee
, sp_employee
, datagrid_employees
, sp_worksessions
, menu_worksession
and grid_selectedEmployee
(assuming that they are not part of any ItemsControl
) to acquire it automatcially.
Once the data context is acquired, its just the matter of mapping to the relevant object from the data context using Binding
and Path
.
Below example will give you the idea... (its for illustration purpose only)
<Window ... >
<Menu ItemsSource="{Binding MenuItems}" ... />
<StackPanel>
<DataGrid ItemsSource="{Binding Employees}"/>
<DockPanel DataContext="{Binding SelectedEmployee}">
<TextBlock Text="{Binding Name}"/>
</DockPanel>
</StackPanel>
</Window>
Code Behind
public MainWindow()
{
InitializeComponent();
//// Now every item in your `Window` hierarchy above autotmatically gets
//// the data context as the instance of EmployeeViewModel class.
this.DataContext = new EmployeeViewModel();
}
So below is the pseudo object hierarchy structure of your view model and model ...
EmployeeViewModel Class
-> Property List<MenuItem> MenuItems
-> Property List<Employee> Employees
-> Property Employee SelectedEmployee
Employee Class
-> Property String Name
Upvotes: 1
Reputation: 12366
Why can't you set the datacontext in xaml?
<DataGrid Name="grid_selectedEmployee" DataContext="{Binding EmployeeViewModel.SelectedEmployee}">
<DataGrid Name="datagrid_worksessions" ItemSource="{Binding SelectedWorkSessions}" >
Just make sure SelectedEployee is DependancyProperty or EmployeeViewModel implements INotifyProperty changed.
Upvotes: 0