I want to create a ContextMenu for TreeViewItems and pass a parameter as TreeViewItem data context (not TreeView data context).
I'm getting the error below for the command binding. What is the problem?
I also tried TreeViewItem, MenuItem and ContextMenu as AncestorType.
I also tried AncestorLevel as 1, 2, 3, but it did not change the result.
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.TreeView', AncestorLevel='1''. BindingExpression:Path=DataContext.DeleteFolderCommand; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')
Here is my code:
<TreeView ItemsSource="{Binding Folders}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}"
BasedOn="{StaticResource {x:Type TreeViewItem}}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Sil"
Command="{Binding DataContext.DeleteFolderCommand,RelativeSource={RelativeSource AncestorType={x:Type TreeView},Mode=FindAncestor}}"
CommandParameter="{Binding }"></MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>