开发者

Wpf TreeView Get Item from ContextMenu

开发者 https://www.devze.com 2023-02-18 04:47 出处:网络
Hi I got the following Treeview, with a ContextMenu with the following MenuItems DoSomeThingOnItem and DoAnotherThingOnItem.

Hi I got the following Treeview, with a ContextMenu with the following MenuItems DoSomeThingOnItem and DoAnotherThingOnItem.

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>

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.


See if this is related:
Find Bound Item from TreeViewItem from ContextMenu

0

精彩评论

暂无评论...
验证码 换一张
取 消