none
How to open a treeview node on single click instead of double click ?

    Question

  • Dear all,

    Is there a way to change the default behaviour of the WPF treeview control based on the expand of items node.
    So far the default behaviour is based on a double click event.

    In my customer application the user interface will be control through touch screen, so double click with touch screen is not easy at all.

    for that simple reason is there a way to get that node expand based on a single click event ?

    Thnaks for the info

    regards
    serge
    Your experience is build from the one of others
    Tuesday, March 10, 2009 10:34 AM

Answers

  • Hi Serge,

    a few days ago a similar question was posted. Here is the sample code.

    You run into a problem with the DoubleClick event, my solution was to derive a custom Control from Treeview to prefent it. :(

    XAML:
    <Window x:Class="DummyProject.Window7" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Window7" Height="300" Width="300" 
        xmlns:local="clr-namespace:DummyProject">  
        <Grid> 
            <local:TreeViewEx x:Name="_treeView1" MouseUp="_treeView1_MouseUp">  
                <TreeViewItem Header="Employee1">  
                    <TreeViewItem Header="Jesper"/>  
                    <TreeViewItem Header="Aaberg"/>  
                    <TreeViewItem Header="12345"/>  
                </TreeViewItem> 
                <TreeViewItem Header="Employee2">  
                    <TreeViewItem Header="Dominik"/>  
                    <TreeViewItem Header="Paiha"/>  
                    <TreeViewItem Header="98765"/>  
                </TreeViewItem> 
     
            </local:TreeViewEx> 
        </Grid> 
    </Window> 
     

    C#

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Data;  
    using System.Windows.Documents;  
    using System.Windows.Input;  
    using System.Windows.Media;  
    using System.Windows.Media.Imaging;  
    using System.Windows.Shapes;  
     
    namespace DummyProject  
    {  
        /// <summary>  
        /// Interaction logic for Window7.xaml  
        /// </summary>  
        public partial class Window7 : Window  
        {  
            public Window7()  
            {  
                InitializeComponent();  
            }         
     
            private void _treeView1_MouseUp(object sender, MouseButtonEventArgs e)  
            {             
                TreeView tv = sender as TreeView;  
                TreeViewItem tvi = tv.SelectedItem as TreeViewItem;  
                tvi.IsExpanded = !tvi.IsExpanded;             
            }  
     
            private void _treeView1_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)  
            {  
                e.Handled =true;  
                  
            }  
        }  
     
        public class TreeViewEx : TreeView  
        {         
            protected override void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)  
            {  
                e.Handled = true;  
                //base.OnPreviewMouseDoubleClick(e);  
            }  
        }  
     
    }  
     

    Hope that helps
    Tuesday, March 10, 2009 3:43 PM
  • Hi Serge,

    use normal TreeView instead of the TreeViewEx and double click on a node, you will see a strange behavior.  The node is expanding and collapsing.

    The TreeViewEx prevents the DoubleClick event you got two MouseUp instead.

    Thursday, March 12, 2009 10:18 AM

All replies

  • Hi Serge,

    a few days ago a similar question was posted. Here is the sample code.

    You run into a problem with the DoubleClick event, my solution was to derive a custom Control from Treeview to prefent it. :(

    XAML:
    <Window x:Class="DummyProject.Window7" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Window7" Height="300" Width="300" 
        xmlns:local="clr-namespace:DummyProject">  
        <Grid> 
            <local:TreeViewEx x:Name="_treeView1" MouseUp="_treeView1_MouseUp">  
                <TreeViewItem Header="Employee1">  
                    <TreeViewItem Header="Jesper"/>  
                    <TreeViewItem Header="Aaberg"/>  
                    <TreeViewItem Header="12345"/>  
                </TreeViewItem> 
                <TreeViewItem Header="Employee2">  
                    <TreeViewItem Header="Dominik"/>  
                    <TreeViewItem Header="Paiha"/>  
                    <TreeViewItem Header="98765"/>  
                </TreeViewItem> 
     
            </local:TreeViewEx> 
        </Grid> 
    </Window> 
     

    C#

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Data;  
    using System.Windows.Documents;  
    using System.Windows.Input;  
    using System.Windows.Media;  
    using System.Windows.Media.Imaging;  
    using System.Windows.Shapes;  
     
    namespace DummyProject  
    {  
        /// <summary>  
        /// Interaction logic for Window7.xaml  
        /// </summary>  
        public partial class Window7 : Window  
        {  
            public Window7()  
            {  
                InitializeComponent();  
            }         
     
            private void _treeView1_MouseUp(object sender, MouseButtonEventArgs e)  
            {             
                TreeView tv = sender as TreeView;  
                TreeViewItem tvi = tv.SelectedItem as TreeViewItem;  
                tvi.IsExpanded = !tvi.IsExpanded;             
            }  
     
            private void _treeView1_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)  
            {  
                e.Handled =true;  
                  
            }  
        }  
     
        public class TreeViewEx : TreeView  
        {         
            protected override void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)  
            {  
                e.Handled = true;  
                //base.OnPreviewMouseDoubleClick(e);  
            }  
        }  
     
    }  
     

    Hope that helps
    Tuesday, March 10, 2009 3:43 PM
  • Thnaks for help
    This is what I am looking for but could you explain a bit what make the fact that it ovewrite the double click ?
    Your experience is build from the one of others
    Thursday, March 12, 2009 9:58 AM
  • Hi Serge,

    use normal TreeView instead of the TreeViewEx and double click on a node, you will see a strange behavior.  The node is expanding and collapsing.

    The TreeViewEx prevents the DoubleClick event you got two MouseUp instead.

    Thursday, March 12, 2009 10:18 AM
  • yes but where this TreeViewEx comes from ?
    This is an other control in WPF ?
    Your experience is build from the one of others
    Thursday, March 12, 2009 10:38 AM
  • Oups sorry, I am blind this morning. Found it :-(
    Your experience is build from the one of others
    Thursday, March 12, 2009 10:40 AM
  • Argss still one thing sorry .

    Your initial code is as follow :

    protected override void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)

    {

    e.Handled = true;

    //base.OnPreviewMouseDoubleClick(e);

    }

    What heppen by doing the e.handle =true ?
    because if I comment out this line , then I get the default behaviour of treeview with double click but having it remove the double clik.

    i try to understand
    thnaks


    Your experience is build from the one of others
    Thursday, March 12, 2009 10:47 AM
  • When setting the e.Handled to true, the event still bubbles up the route but the Handlers are not called.

    You find more information here http://msdn.microsoft.com/en-us/library/ms742806.aspx.

    P.S.
    If you want that the Handler is also called when e.Handled=true, than you have to set HandledEventsToo=true on the handler.
    Thursday, March 12, 2009 11:18 AM