1

I have a TreeView I'm populating and adding a ContextMenu to each item. The problem is in my ViewModel the TreeView ItemSource is bound to a property on the ViewModel itself. When I attempt to reference some property on the ViewModel again I can't seem to get it to work.

<TreeView Grid.ColumnSpan="1" Grid.Row="1" HorizontalAlignment="Stretch" ItemsSource="{Binding ModelItems}" SelectedTreeItem="{Binding SelectedItem, Mode=TwoWay}" VerticalAlignment="Stretch" Grid.RowSpan="3" Margin="5">
<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Models}">
        <TextBlock Text="{Binding Header, Mode=TwoWay}"  ToolTip="{Binding Tooltip, Mode=TwoWay}">
            <TextBlock.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Server" Visibility="{Binding Path=IsServerVisible}">
                        <MenuItem Header="Add" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Windows:MainWindow}}, Path=ViewModel:ViewModel.AddServerCommand}"/>
                        <MenuItem Header="Edit" />
                        <MenuItem Header="Delete" />
                    </MenuItem>
                    <MenuItem Header="Config" Visibility="{Binding Path=IsConfigVisible}">
                        <MenuItem Header="Fetch" />
                        <MenuItem Header="Edit" />
                        <MenuItem Header="Save" />
                    </MenuItem>     
                </ContextMenu>
            </TextBlock.ContextMenu>
        </TextBlock>
    </HierarchicalDataTemplate>                
</TreeView.ItemTemplate>

A previous post on StackOverflow pointed me in the direction of using the RelativeSource to bind correctly to my ViewModel on the MainWindow. However when I run the application the command is not working and the Output window is not generating any binding or xaml errors that I can see.

Basically the Visibility bindings work because those properties exist on the "Models" items. However I want everything to be moved to the ViewModel especially the Command.

Can anyone spot what I've done incorrectly here?

Tada
  • 1,585
  • 2
  • 16
  • 26
  • Actually I was able to get the error message to output: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Windows.MainWindow', AncestorLevel='1''. BindingExpression:Path=ViewModel:ViewModel.AddServerCommand; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand') – Tada Nov 15 '12 at 02:29

3 Answers3

3

The key thing to remember here is context menus are not part of the visual tree.

Therefore they don't inherit the same source as the control they belong to for binding. The way to deal with this is to bind to the placement target of the ContextMenu itself. But since you want to bind it for Command in ViewModel class, place the DataContext in Tag of your TextBlock and use in your command Binding like this -

<HierarchicalDataTemplate ItemsSource="{Binding Models}">
    <TextBlock Text="{Binding Header}"
               Tag="{Binding DataContext, RelativeSource=
               {RelativeSource Mode=FindAncestor, AncestorType=Window}}">
         <TextBlock.ContextMenu>
              <ContextMenu>
                  <MenuItem Header="Server" Command="{Binding 
                            PlacementTarget.Tag.AddServerCommand,
                            RelativeSource={RelativeSource Mode=FindAncestor, 
                            AncestorType=ContextMenu}}"/>
               </ContextMenu>
          </TextBlock.ContextMenu>
      </TextBlock>
</HierarchicalDataTemplate>

Use for other bindings similarly like above and it will work how you want.

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • I feel like I half understand why this works and half I do not. I accepted this answer because it is the first one I've tried that works. So I guess the Tag on the TextBlock is creating a reference to my DataContext and then the MenuItem is using it plus relative source to find the Tag reference. Pretty neat thank you. – Tada Nov 15 '12 at 13:53
  • Yeah you got it right. `Tag` holding the reference to your DataContext. – Rohit Vats Nov 15 '12 at 13:56
  • 1
    Thanks again, I thought I was going to have another Headache over this again today! – Tada Nov 15 '12 at 13:56
0

Because you didn't post the full .xaml, lets use the datacontext of the treeview. Because The RelativeSource is giving us access to the TreeView, you need to use DataContext in the Binding Path since that is a reference to your ViewModel.

Command="{Binding Path="DataContext.AddServerCommand" RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}}"
Lee O.
  • 3,212
  • 2
  • 26
  • 36
  • So I've tried this as well and it still can't resolve to a valid reference. I know I'm using the correct syntax and all. Something odd is happening. – Tada Nov 15 '12 at 02:49
  • http://stackoverflow.com/questions/1013558/elementname-binding-from-menuitem-in-contextmenu a couple of those solutions should work. I'd personally go for the .xaml binding one with 4 upvotes if it works. – Lee O. Nov 15 '12 at 05:56
0

Instead of

<MenuItem Header="Add" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Windows:MainWindow}}, Path=ViewModel:ViewModel.AddServerCommand}"/>

Try

<MenuItem Header="Add" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Windows:MainWindow}}, Path=DataContext.AddServerCommand}"/>

Isn't your ViewModel your DataContext?

Alan
  • 7,875
  • 1
  • 28
  • 48