I've got a Treeview setup in a WPF (trying to mostly follow MVVM) application with a HierarchicalDataTemplate setup...for each item I've got a TextBlock and a couple buttons, as such:
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="13"/>
<ColumnDefinition Width="13"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Name}" Grid.Column="0">
<TextBlock.ContextMenu>
<ContextMenu DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Mode=Self}}">
<MenuItem Header="Delete Document" ToolTip="{Binding IDPath}" Command="{Binding DeleteDocumentCommand}"
CommandParameter="{Binding IDPath}" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
<Button Content="X" Grid.Column="1" Height="12" Width="12" HorizontalAlignment="Center" ToolTip="Delete Document" Command="{Binding DataContext.DeleteDocumentCommand, RelativeSource={RelativeSource AncestorType=TreeView}}" CommandParameter="{Binding IDPath}" />
<Button Content="X" Grid.Column="2" Height="12" Width="12" HorizontalAlignment="Center" ToolTip="Move Document" Command="{Binding DataContext.MoveDocumentCommand, RelativeSource={RelativeSource AncestorType=TreeView}}" CommandParameter="{Binding IDPath}" />
</Grid>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
All of these bindings are working well, except for the Command on the context menu MenuItem. I have looked through several solutions here on SO (above is one attempt, I also tried to set the "Tag" of the parent ...which also did not help (using PlacementTarget.Tab instead of DataContext).
I must be missing something simple but I just can't figure it out. I setup a binding to the 's Tooltip property, just to verify that the IDPath is bound correct (it is)...but the DeleteDocumentCommand just refuses to fire.
If anybody has any ideas I'd appreciate it, this has been killing me for the last few hours...thanks!
Edit, here's an example with the "Tag" concept to kind of transfer the DataContext from an object in the viewtree (The TextBlock) and make it accessible to the ContextMenu. With this method, again, everything binds fine (MenuItem->Tooltip for example), but still the method won't fire...
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="13"/>
<ColumnDefinition Width="13"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Name}" Grid.Column="0" Tag="{Binding DataContext, RelativeSource={RelativeSource Self}}">
<TextBlock.ContextMenu>
<ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Delete Document" ToolTip="{Binding IDPath}" Command="{Binding DeleteDocumentCommand}"
CommandParameter="{Binding IDPath}" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
<Button Content="X" Grid.Column="1" Height="12" Width="12" HorizontalAlignment="Center" ToolTip="Delete Document" Command="{Binding DataContext.DeleteDocumentCommand, RelativeSource={RelativeSource AncestorType=TreeView}}" CommandParameter="{Binding IDPath}" />
<Button Content="X" Grid.Column="2" Height="12" Width="12" HorizontalAlignment="Center" ToolTip="Move Document" Command="{Binding DataContext.MoveDocumentCommand, RelativeSource={RelativeSource AncestorType=TreeView}}" CommandParameter="{Binding IDPath}" />
</Grid>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
My instincts tell me that since the Tooltip binding it working, that I am successfully getting the DataContext down to the ContextMenu level...and yet, the Command won't fire...I'm not sure what else I'm missing here.
Edit Again: Ok...I see now that the DataContext that I'm gaining access via the "Tag" attribute is the collection that is populating the treeview...not the actual ViewModel object itself...so I'm assuming that's why the command doesn't work. So I'm not sure how to get the correct DataContext at this point...should it be coming from the top-most window?