I've a TreeView inside a ScrollViewer. When I turn the mouse wheel on the treeview, treeview is not sliding and it stays at the same point. But when I move the scrollbar, I can slide the treeview.
I'm pretty sure that treeview has a scrollviewer but my point is to use an other scrollviewer is, I've a scrollviwer template and I used this template all over my app. That's why I want to use this template again.
Here is the code:
<ScrollViewer Template="{DynamicResource AppleStyleScrollBarStyle}">
<TreeView>
<TreeViewItem Header="TreeViewItem"/>
<TreeViewItem Header="TreeViewItem"/>
<TreeViewItem Header="TreeViewItem"/>
<TreeViewItem Header="TreeViewItem"/>
</TreeView>
</ScrollViewer>
This answer helped me: WPF Remove ScrollViewer from TreeView
https://serialseb.com/blog/2007/09/03/wpf-tips-6-preventing-scrollviewer-from/
Just subscribe to your treeView's PreviewMouseWheel event:
// https://serialseb.com/blog/2007/09/03/wpf-tips-6-preventing-scrollviewer-from/
private void treeView_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if (sender is TreeView && !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);
}
}
TreeView
default template already contains ScrollViewer
. When you wrap it in another ScrollViewer
the one inside TreeView
does nothing but still handles some mouse events like mouse wheel. Instead you can create implicit Style
for ScrollViewer
in TreeView.Resources
<TreeView>
<TreeView.Resources>
<Style TargetType="{x:Type ScrollViewer}">
<Setter Property="Template" Value="{DynamicResource AppleStyleScrollBarStyle}"/>
</Style>
</TreeView.Resources>
<TreeViewItem Header="TreeViewItem"/>
<TreeViewItem Header="TreeViewItem"/>
<TreeViewItem Header="TreeViewItem"/>
<TreeViewItem Header="TreeViewItem"/>
</TreeView>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With