c# - Wpf TreeView 从上下文菜单获取项目

标签 c# .net wpf treeview

嗨 我得到了以下 Treeview,其中包含带有以下 MenuItems DoSomeThingOnItem 和 DoAnotherThingOnItem 的 ContextMenu。

但是我需要做什么才能获取单击 contextmenuitem 的项目?

<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>

为了更清楚地说明,这是我的上下文菜单

 <ContextMenu>
   <MenuItem Header="DoSomeThingOnItem"></MenuItem>
   <MenuItem Header="DoAnotherThingOnItem"></MenuItem>
 </ContextMenu>

我已经尝试过 Click 事件,但它只是给了我一个

MenuItem contextMenuItem = (MenuItem)sender;

我无法找到从中获取该元素的方法。

最佳答案

关于c# - Wpf TreeView 从上下文菜单获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5391821/

相关文章:

c# - 是否有内置的 c# 类来定义具有开始日期和结束日期的时间窗口?

c# - 这是 ASP.NET 模型绑定(bind)中的错误吗?

.net - 转义 SQL 查询 - 支持 .Net 中的不同数据库平台

c# - DotNetBrowser 中的 Silverlight 支持

c# - 如何通过 xmlreader 从资源中读取 xml?

c# - 使用超过 2^31 个元素的虚拟化集合进行 UI 虚拟化

c# - .NET 未检测到任何 SQL Server 实例

c# - 为什么我不能在 XAML 中定义 ResourceDictionary 并自行实例化它?

C# 有枚举的枚举

.net - 下载 .NET 4 源代码