views:

108

answers:

1

I was wondeirng if its possible to turn off the treeview's scrollviewer easily. I have a user controil with a grid and one of the cells has a few treeviews inside of a stackpanel. the height of the control sizes automatically depending on the height of the treeviews, so there is no need for a scrollbar. The problem is i have a bunch of these in a listbnopx with its own scrollviewer, but when i am using the mouse wheel, scrolling stops when you are over a treeview. this is because the treeview has its own scrollviewer which steals the mousewheel.

I know this is probably possible by overriding the control template, but im hoping there is an easier way.

+2  A: 

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>
Quartermeister
Hmm, it seems my last comment didnt work. Anyways, thanks! I tried both. The xaml control template worked perfectly, but the PreviewMouseWheel still got "stuck" a few times while scrolling. I will go with the xaml solution.
+1 for XAML Solution (also worked perfect with the same problem)
JanW