0

I'm wondering how to pass the selected item to a command from a treeview / HierarchicalDataTemplate ?

Here is the code that I have so far, it displays the context menu but I haven't bound the commands to it yet. The command binding is the easy part, but how do I tell which node it came from ?

<HierarchicalDataTemplate 
    DataType="{x:Type viewModel:UsersViewModel}" 
    ItemsSource="{Binding Children}">
    <StackPanel Orientation="Horizontal">
        <Image Width="16" Height="16" Margin="3,0" Source="Images\Region.png" />
        <TextBlock Text="{Binding UserName}">
            <TextBlock.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Edit" />
                        <MenuItem Header="Delete"/>
                     </ContextMenu>
                </TextBlock.ContextMenu>
        </TextBlock>
    </StackPanel>
</HierarchicalDataTemplate>
H.B.
  • 166,899
  • 29
  • 327
  • 400
rreeves
  • 2,408
  • 6
  • 39
  • 53

1 Answers1

1

Just {Binding} should be the whole item.

(To pass it to the Command bind the CommandParameter, in Execute and CanExecute it will become the method parameter (which you then need to cast to your item-type))

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Could you provide a sample ? I'm not able to get it to work. But from my research I see that this is the correct way to do it. I'll mark as answered for now. – rreeves Feb 06 '12 at 22:40
  • @BatMasterson: [See this answer](http://stackoverflow.com/questions/6914867/why-cant-i-use-datacontext-binding-for-my-context-menu/6914964#6914964), it should clear the problem you probably have. – H.B. Feb 06 '12 at 22:44