Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ControlTemplate for scrolling TreeView Control

Tags:

c#

wpf

I'm using a the TreeView control and it scrolls automatically to left-align TreeViewItem when one of them is clicked. I've gone looking at my Styles and ControlTemplates, but I haven't found anything. Is there a default ControlTemplate that causes this? I want to disable it.

like image 331
Ben Collins Avatar asked Oct 28 '08 21:10

Ben Collins


1 Answers

The items scroll because the ScrollViewer calls BringIntoView() on them. So one way to avoid scrolling is to suppress the handling of the RequestBringIntoView event. You can try that out quickly by subclassing TreeView and instantiating this control instead:

public class NoScrollTreeView : TreeView
{
    public class NoScrollTreeViewItem : TreeViewItem
    {
        public NoScrollTreeViewItem() : base()
        {
            this.RequestBringIntoView += delegate (object sender, RequestBringIntoViewEventArgs e) {
                e.Handled = true;
            };
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new NoScrollTreeViewItem();
        }
    }
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new NoScrollTreeViewItem();
    }
}
like image 196
brian sharon Avatar answered Oct 06 '22 00:10

brian sharon