none
如何屏蔽控件的默认行为(例如TreeView对MouseDoubleClick的响应)? RRS feed

  • 问题

  •          

    如题,想对TreeView的MouseDoubleClick事件进行自定义的操作,比如双击后展开所有子级TreeViewItem,想到的方法是自定义一个TreeView,继承TreeView并重写OnMouseDoubleClick过程:如下

    Public Class MyTreeView
        Inherits TreeView
    
        Protected Overrides Sub OnMouseDoubleClick(e As MouseButtonEventArgs)
            'MyBase.OnMouseDoubleClick(e)
             MsgBox("bingo")
        End Sub
    
    End Class

    原本以为注释掉MyBase.OnMouseDoubleClick(e)后就不会引发TreeView双击后的默认行为(展开当前TreeViewItem),但是发现无论注释 与否这个默认行为一直存在,求解?
         BTW:WPF应用程序。

       
    2014年9月5日 14:28

答案

  • 楼主 你好,可以这样操作,

    XAML:

    <TreeView DockPanel.Dock="Left" TreeViewItem.PreviewMouseDoubleClick="TreeViewItem_PreviewMouseDoubleClick">
        <TreeViewItem Header="Node Level 1" IsExpanded="True" >
            <TreeViewItem Header="Node Level 2.1" >
                <TreeViewItem Header="MyItem" />
            </TreeViewItem>
            <TreeViewItem Header="Node Level 2.2">
                <TreeViewItem Header="MyItem" />
            </TreeViewItem>
        </TreeViewItem>
    </TreeView>

    C# Code:

    private void TreeViewItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        //this will suppress the event that is causing the nodes to expand/contract 
        e.Handled = true;
    }

    更多信息,请参考:http://stackoverflow.com/questions/6037883/how-to-disable-double-click-behaviour-in-a-wpf-treeview

    希望能够帮到你!

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年9月8日 3:32
    版主

全部回复

  •     自己顶一下,求达人解答。
    2014年9月6日 12:22
  • 楼主 你好,可以这样操作,

    XAML:

    <TreeView DockPanel.Dock="Left" TreeViewItem.PreviewMouseDoubleClick="TreeViewItem_PreviewMouseDoubleClick">
        <TreeViewItem Header="Node Level 1" IsExpanded="True" >
            <TreeViewItem Header="Node Level 2.1" >
                <TreeViewItem Header="MyItem" />
            </TreeViewItem>
            <TreeViewItem Header="Node Level 2.2">
                <TreeViewItem Header="MyItem" />
            </TreeViewItem>
        </TreeViewItem>
    </TreeView>

    C# Code:

    private void TreeViewItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        //this will suppress the event that is causing the nodes to expand/contract 
        e.Handled = true;
    }

    更多信息,请参考:http://stackoverflow.com/questions/6037883/how-to-disable-double-click-behaviour-in-a-wpf-treeview

    希望能够帮到你!

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年9月8日 3:32
    版主
  •      不好意思,前两天有事现在才回复,先谢谢了,等我仔细实验下,有问题再来请教,再次感谢。
    2014年9月18日 12:55