I have a TreeView whose DataContext is my ViewModel. The ViewModel has a property MyProp
. Inside the TreeView, I have defined an ItemContainerStyle with a DataTrigger:
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Style.Triggers>
<DataTrigger Binding={MyProp} Value="...">
<!-- does not work because DataContext is the Item, not the ViewModel -->
<Setter Property="Focusable" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
But since the ItemContainerStyle has an Item as its DataContext and not the ViewModel, the Binding in the DataTrigger does not work. So how do I tell the Binding that I want to bind to ViewModel.MyProp
? It seems I cannot set DataContext
in Styles and Triggers, so how else can I do it?
Did you tried to take your relative source:
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.MyProp}" Value=" ">