Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select node in treeView on right click MVVM

Tags:

wpf

xaml

treeview

I want to select a node of tree view on right click. I am using MVVM pattern and don't want to achieve this in code behind. Here is my XAML for tree view.

<TreeView Margin="5,0,0,5"  ItemsSource="{Binding TreePads}">
<TreeView.ItemContainerStyle >
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsSelected" Value="{Binding DataContext.IsSelected, Mode=TwoWay,  RelativeSource={RelativeSource Self}}" />
        <Setter Property="ContextMenu">
            <Setter.Value>
                <ContextMenu  DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" >
                    <MenuItem IsEnabled="{Binding RenameMenuEnabled}" Header="Rename" Command="{Binding RenameCommand}" />
                </ContextMenu>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
            </Trigger>
        </Style.Triggers>
    </Style>
</TreeView.ItemContainerStyle >
<TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type vm:TreePad}" ItemsSource="{Binding Members, Mode=TwoWay}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding PadName}"/>
        </StackPanel>
    </HierarchicalDataTemplate>
</TreeView.Resources>

like image 529
WAQ Avatar asked Aug 03 '13 06:08

WAQ


1 Answers

You could define a DependencyProperty. Below I have shared a sample app which uses a dependency property to achieve this.

TreeViewExtension.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace WpfApplication1
{
    public static class TreeViewExtension
    {
        public static readonly DependencyProperty SelectItemOnRightClickProperty = DependencyProperty.RegisterAttached(
           "SelectItemOnRightClick",
           typeof(bool),
           typeof(TreeViewExtension),
           new UIPropertyMetadata(false, OnSelectItemOnRightClickChanged));

        public static bool GetSelectItemOnRightClick(DependencyObject d)
        {
            return (bool)d.GetValue(SelectItemOnRightClickProperty);
        }

        public static void SetSelectItemOnRightClick(DependencyObject d, bool value)
        {
            d.SetValue(SelectItemOnRightClickProperty, value);
        }

        private static void OnSelectItemOnRightClickChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            bool selectItemOnRightClick = (bool)e.NewValue;

            TreeView treeView = d as TreeView;
            if (treeView != null)
            {
                if (selectItemOnRightClick)
                    treeView.PreviewMouseRightButtonDown += OnPreviewMouseRightButtonDown;
                else
                    treeView.PreviewMouseRightButtonDown -= OnPreviewMouseRightButtonDown;
            }
        }

        private static void OnPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject);

            if (treeViewItem != null)
            {
                treeViewItem.Focus();
                e.Handled = true;
            }
        }

        public static TreeViewItem VisualUpwardSearch(DependencyObject source)
        {
            while (source != null && !(source is TreeViewItem))
                source = VisualTreeHelper.GetParent(source);

            return source as TreeViewItem;
        }
    }
}

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mvvmhelper="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TreeView mvvmhelper:TreeViewExtension.SelectItemOnRightClick="true">
            <TreeViewItem Header="One"/>
            <TreeViewItem Header="Two"/>

            <TreeViewItem Header="Three"/>
            <TreeView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Menu1"/>
                    <MenuItem Header="Menu2"/>
                </ContextMenu>
            </TreeView.ContextMenu>
        </TreeView>
    </Grid>
</Window>
like image 117
Anand Murali Avatar answered Sep 22 '22 19:09

Anand Murali