0

I am working on a small project which needs to add the treeview items to a listbox, I managed to do this with my treeview:

<HierarchicalDataTemplate DataType="{x:Type data:Category}" ItemsSource="{Binding Path=Products}">
        <TextBlock Text="{Binding Path=CategoryName}" AllowDrop="True"/>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate DataType="{x:Type data:Product}" >
        <TextBlock Text="{Binding Path=ModelName}" AllowDrop="True"/>
    </HierarchicalDataTemplate>

And i also tried to add a contextmenu to the treeviewitem like this:

<TreeView x:Name="tv_Project" Margin="5">
                                    <TreeView.ItemContainerStyle>
                                        <Style TargetType="{x:Type TreeViewItem}">
                                            <EventSetter Event="TreeViewItem.PreviewMouseRightButtonDown" Handler="TreeViewItem_PreviewMouseRightButtonDown"/>
                                        </Style>
                                    </TreeView.ItemContainerStyle>
                                    <TreeView.ContextMenu>
                                        <ContextMenu>
                                            <MenuItem Header="Add To Project" Click="MenuItem_OnClick" />
                                        </ContextMenu>
                                    </TreeView.ContextMenu>
                                </TreeView>

The Problem is i just want to add the Eventto my child-node(in my project ModelName) not including parent-node(in my project CategoryName), What should i do?Thanks!

Here is my backend code:

        private void TreeViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        var treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
        if (treeViewItem != null)
        {
            treeViewItem.Focus();
            e.Handled = true;
        }
    }

    static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
    {
        while (source != null && source.GetType() != typeof(T))
            source = VisualTreeHelper.GetParent(source);

        return source;
    }
    private void MenuItem_OnClick(object sender, RoutedEventArgs e)
    {
        var item = this.tv_Project.SelectedItem;
        if (item != null)
        {
            lb_Configuration.Items.Add(item);
        }
    }
Lee Barry
  • 37
  • 9
  • Why don't you simply add menu to hierarchical data template? See [this question](http://stackoverflow.com/q/13420994/1997232). – Sinatr May 12 '17 at 08:01
  • Thanks for your reply,This is a little bug in the question you mentioned.When you left_Click the child-node and then right_Click, it will be right,but if you directly right_click the child-node, actually choose the parent-node. – Lee Barry May 12 '17 at 08:42
  • If you look for accepted answer there you will see how to add menu to item template. Do it for `Product` template only and then you won't have context menu for `Category`. – Sinatr May 12 '17 at 08:47
  • @Sinatr Actually i did that only for **Product** like this:` ` – Lee Barry May 12 '17 at 09:35

0 Answers0