0
votes

i want to make a CommandParameter Binding to another ViewModel then the Command is. The Commands Binding steps out of the collection of the treeview to the ViewModel that contains the "Items" - Collection. But the CommandParameter should pass the ViewModel from the "Children". If i just do {Binding} it passes the wrong ViewModel. (the one where the "Items" Collection is)

<TreeView 
                ItemsSource="{Binding Items}">
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                        <StackPanel Orientation="Horizontal" 
                                    CanHorizontallyScroll="True">
                            <CheckBox VerticalAlignment="Center" 
                                      IsChecked="{Binding IsSelected}"/>
                            <TextBlock VerticalAlignment="Center" 
                                       FontSize="14"
                                       Text="{Binding Name}"/>
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
                <TreeView.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Set Script-Root"
                                  Command="{Binding Path=DataContext.SomeCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                                  CommandParameter="{Binding HOW_TO_VIEWMODEL_of_TREEVIEW-ITEMS???}"/>
                    </ContextMenu>
                </TreeView.ContextMenu>
            </TreeView>
1

1 Answers

0
votes

To make this work, you need to set your ContextMenu on the TreeViewItem instead.

That will give you the ability to set:

CommandParameter="{Binding}"

Which will be the DataContext of the TreeViewItem, and:

Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeView}}, Path=DataContext.SomeCommand}"

Which will get the DataContext of the TreeView.

I hope this helps.