WPF树视图获取项目的文本菜单 [英] Wpf TreeView Get Item from ContextMenu
问题描述
喜 我得到了下面的树视图,用文本菜单具有以下的MenuItems DoSomeThingOnItem和DoAnotherThingOnItem。
Hi I got the following Treeview, with a ContextMenu with the following MenuItems DoSomeThingOnItem and DoAnotherThingOnItem.
但是,我需要做的,让我得到了项目,其中的ContextMenuItem被点击了?
But what do I need to do, for me to get the Item, where the contextmenuitem was clicked?
<TreeView Name="treeView1" ItemsSource="{Binding Regions}" AllowDrop="True" PreviewMouseRightButtonDown="TreeView1PreviewMouseRightButtonDown">
<TreeView.ItemContainerStyle>
<!-- This Style binds a TreeViewItem to a TreeViewItemViewModel.-->
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="AllowDrop" Value="True" />
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
<Setter Property="FontWeight" Value="Normal"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type ViewModel:ProductViewModel}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin=" 3,0" Source="Images\item.png"/>
<TextBlock Text="{Binding Name}"></TextBlock>
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="DoSomeThingOnItem"></MenuItem>
<MenuItem Header="DoAnotherThingOnItem"></MenuItem>
</ContextMenu>
</StackPanel.ContextMenu>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
只是为了更清楚,这是我的文本菜单
Just to make it more clear, here is my contextmenu
<ContextMenu>
<MenuItem Header="DoSomeThingOnItem"></MenuItem>
<MenuItem Header="DoAnotherThingOnItem"></MenuItem>
</ContextMenu>
我已经尝试过的Click事件,但它只是给了我一个
I have already tried with the Click event, but It just gives me a
MenuItem contextMenuItem = (MenuItem)sender;
和我不能想出一个办法来从获得该项目。
And I can't figure out a way to get the item from that.
推荐答案
看看这是相关的:
<一href="http://stackoverflow.com/questions/3560796/find-bound-item-from-treeviewitem-from-contextmenu">Find一架从树型视图项目从文本菜单
See if this is related:
Find Bound Item from TreeViewItem from ContextMenu
这篇关于WPF树视图获取项目的文本菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!