I am trying to bind a Property that is outside of an Itemscontrol. However that doesn't seem to work.
It seems that in ItemsControl, DataTemplate it refers to what is inside of the collection and not outside of it. I have tried with RelativeResource and Referred to AncestorType for the 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!" />
You need to bind to the DataContext
of the parent ItemsControl
.
<ItemsControl ItemsSource="{Binding Path=IterateProperty}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DataContext.GetThis,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type ItemsControl}}}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With