1

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>

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

it is a perfect workaround solution. Thanks. What would the solution be if the tag property was used for another purpose?
@ErhanUrun Then you could create an attached property and use it the same way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.