Bind a property outside of the Items control in XAML

I am trying to bind a property that is outside the Itemscontrol element. However, this does not work.

It seems that in the ItemsControl, DataTemplate, this refers to what is inside the collection, and not outside of it. I tried with RelativeResource and referenced AncestorType for ViewModel.

Code (VM):

public class Test { public string GetThis {get{return "123";} set{}} public List<string> IterateProperty {get; set;} } 

XAML (View):

 <ItemsControl ItemsSource="{Binding Path=IterateProperty}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="I want to bind the string property GetThis!" /> 
+7
c # wpf xaml caliburn.micro
source share
2 answers

You need to bind to the DataContext parent ItemsControl .

 <ItemsControl ItemsSource="{Binding Path=IterateProperty}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding DataContext.GetThis, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}" /> 
+8
source share

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); } } // Using a DependencyProperty as the backing store for TextFromParent. This enables animation, styling, binding, etc... public static readonly DependencyProperty TextFromParentProperty = DependencyProperty.Register("TextFromParent", typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty)); public ObservableCollection<Model> items { get; set; } public MainWindow() { InitializeComponent(); items = new ObservableCollection<Model>(); items.Add(new Model() { IsChecked = true }); items.Add(new Model() { IsChecked = false }); items.Add(new Model() { IsChecked = true }); items.Add(new Model() { IsChecked = false }); TextFromParent = "test"; this.DataContext = this; } } 

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.

enter image description here

+3
source share

All Articles