WPF Remove ScrollViewer from TreeView

后端 未结 1 1172
情深已故
情深已故 2020-12-06 00:44

I was wondering if it is possible to turn off the TreeView\'s ScrollViewer easily.

I have a UserControl with a Grid. One of the Cells has a few TreeViews inside a

相关标签:
1条回答
  • 2020-12-06 01:19

    You can use the technique described here: http://serialseb.blogspot.com/2007/09/wpf-tips-6-preventing-scrollviewer-from.html to prevent the mouse wheel events from being handled by the ScrollViewer. Add PreviewMouseWheel="HandlePreviewMouseWheel" to your TreeView and define HandlePreviewMouseWheel as:

    private void HandlePreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (!e.Handled)
        {
            e.Handled = true;
            var eventArg = new MouseWheelEventArgs(
                e.MouseDevice, e.Timestamp, e.Delta);
            eventArg.RoutedEvent = UIElement.MouseWheelEvent;
            eventArg.Source = sender;
            var parent = ((Control)sender).Parent as UIElement;
            parent.RaiseEvent(eventArg);
        }
    }
    

    Changing the control template to not include a ScrollViewer isn't that hard, though, since the default template for TreeView is pretty simple, and most of the complexity is handling the ScrollViewer. Try doing this:

    <TreeView.Template>
        <ControlTemplate TargetType="TreeView">
            <Border BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    SnapsToDevicePixels="true">
                <ItemsPresenter/>
            </Border>
        </ControlTemplate>
    </TreeView.Template>
    
    0 讨论(0)
提交回复
热议问题