Search code examples
wpfxamlbindingcommandtreeview

Wpf TreeViewItem Contextmenu Command binding not working


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>

Solution

  • A ContextMenu is is not part of the same visual tree as its associated control, so RelativeSource bindings do not work.

    Bind the Tag property of a TreeViewItem to the DataContext of the parent TreeView instead. Then you can access this property through the PlacementTarget property on the context menu, which is set to the corresponding TreeViewItem.

    <TreeView.ItemContainerStyle>
       <Style TargetType="{x:Type TreeViewItem}"
              BasedOn="{StaticResource {x:Type TreeViewItem}}">
          <Setter Property="Tag" Value="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}"/>
          <Setter Property="ContextMenu">
             <Setter.Value>
                <ContextMenu>
                   <MenuItem Header="Sil"
                             Command="{Binding PlacementTarget.Tag.DeleteFolderCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                             CommandParameter="{Binding}"></MenuItem>
                </ContextMenu>
             </Setter.Value>
          </Setter>
       </Style>
    </TreeView.ItemContainerStyle>