I made a quick and complete example:
<Window x:Class="ParentDataContext.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <DataGrid ItemsSource="{Binding items}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding IsChecked}"></CheckBox> <TextBlock Margin="5" Text="{Binding Path=DataContext.TextFromParent,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid>
The context for each row is set for each object from the linked list. In our case, for each instance of the model from the collection of elements.
To return to the parent DataContext, this syntax is used:
Text="{Binding Path=DataContext.TextFromParent,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
Here is the code:
public partial class MainWindow : Window { public string TextFromParent { get { return (string)GetValue(TextFromParentProperty); } set { SetValue(TextFromParentProperty, value); } }
The dependency property can be defined in ViewModel.
And here is my simple model:
public class Model : INotifyPropertyChanged { private bool _IsChecked; public bool IsChecked { get { return _IsChecked; } set { _IsChecked = value; PropertyChanged(this, new PropertyChangedEventArgs("IsChecked")); } } public event PropertyChangedEventHandler PropertyChanged = delegate { }; }
As a result, you can access the property defined in the parent DataContext.

Olaru mircea
source share