-1

I have a treeview and i want have a menu on right click. On clicking menu item a command should get called with the item. I have tried different solution but keep getting error as below.

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression

My treeview is as below

   <TreeView ItemsSource="{Binding Buildings}" Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" Grid.RowSpan="4">
                    <TreeView.Resources>
                        <HierarchicalDataTemplate  ItemsSource="{Binding Gates}" DataType="{x:Type local:Floor}">

                            <TextBlock Text="{Binding Name}" />
                        </HierarchicalDataTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding Floors}" DataType="{x:Type local:Building}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Name}" >
                                    <TextBlock.ContextMenu>
                                        <ContextMenu>
                                            <ContextMenu.ItemContainerStyle>
                                                <Style TargetType="MenuItem">
                                                    <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.EditCmd}"/>
                                                </Style>
                                            </ContextMenu.ItemContainerStyle>
                                        </ContextMenu>
                                    </TextBlock.ContextMenu>
                                </TextBlock>
                            </StackPanel>
                        </HierarchicalDataTemplate>
                        <DataTemplate DataType="{x:Type local:Gate}">
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </TreeView.Resources>
                </TreeView>

Also in viewmodel

   private ICommand editCmd;
    public ICommand EditCmd
    {
        get
        {
            if (editCmd == null)
                editCmd = new DelegateCommand(EditObjectFunction);
            return editCmd;
        }
    }

I have tried different solution like This but could not solve the problem

RPK
  • 53
  • 1
  • 13
  • Possible duplicate of [RelativeSource binding from a ToolTip or ContextMenu](https://stackoverflow.com/questions/3668654/relativesource-binding-from-a-tooltip-or-contextmenu) – Fruchtzwerg Aug 19 '17 at 06:56
  • It does not have treeview and I have tried the solution provided there. Thanks. – RPK Aug 19 '17 at 07:37
  • Have you tried Path=Tag.EditCmd ? – SAT Aug 21 '17 at 09:38

1 Answers1

0

The UserControl is not a visual ancestor of the MenuItem since a ContextMenu resides in its own element tree.

But you could bind the Tag property of the TextBlock to the UserControl and then bind the Command property of the MenuItem to the PlacementTarget property of the ContextMenu:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Name}" Tag="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}">
        <TextBlock.ContextMenu>
            <ContextMenu>
                <ContextMenu.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Tag.DataContext.EditCmd}"/>
                    </Style>
                </ContextMenu.ItemContainerStyle>
            </ContextMenu>
        </TextBlock.ContextMenu>
    </TextBlock>
</StackPanel>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • This is the error I am getting with above solution.System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:(no path); DataItem=null; target element is 'TreeView' (Name=''); target property is 'Tag' (type 'Object') – RPK Aug 26 '17 at 10:47
  • Is the TreeView really located inside a UserControl then? I doubt it. Please post your full XAML. – mm8 Aug 28 '17 at 09:22