Disable Mouse Wheel on Itemscontrol in Wpf

disable mouse wheel on itemscontrol in wpf

The answer you have referenced is exactly what is causing your problem, the ListBox (which is composed of among other things a ScrollViewer) inside your ScrollViewer catches the MouseWheel event and handles it, preventing it from bubbling and thus the ScrollViewer has no idea the event ever occurred.

Use the following extremely simple ControlTemplate for your ListBox to demonstrate (note it does not have a ScrollViewer in it and so the MouseWheel event will not be caught) The ScrollViewer will still scroll with the mouse over the ListBox.

<UserControl.Resources>
<ControlTemplate x:Key="NoScroll">
<ItemsPresenter></ItemsPresenter>
</ControlTemplate>
</UserControl.Resources>

<ScrollViewer>
<SomeContainerControl>
<.... what ever other controls are inside your ScrollViewer>
<ListBox Template="{StaticResource NoScroll}"></ListBox>
<SomeContainerControl>
</ScrollViewer>

You do have the option of capturing the mouse when it enters the ScrollViewer though so it continues to receive all mouse events until the mouse is released, however this option would require you to delgate any further mouse events to the controls contained within the ScrollViewer if you want a response...the following MouseEnter MouseLeave event handlers will be sufficient.

private void ScrollViewerMouseEnter(object sender, MouseEventArgs e)
{
((ScrollViewer)sender).CaptureMouse();
}

private void ScrollViewerMouseLeave(object sender, MouseEventArgs e)
{
((ScrollViewer)sender).ReleaseMouseCapture();
}

Neither of the workarounds I have provided are really preferred however and I would suggest rethinking what you are actually trying to do. If you explain what you are trying to achieve in your question I'm sure you will get some more suggestions...

disable mouse wheel scrolling in scrollviewer wpf

you could handle the MouseWheel Event of Custom Canvas so that when the mouse is pointed in your canvas area and the wheeling event accured you set the Handled property of the MouseWheelEventArgs to true :

 private void UIElement_OnMouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
//handler your zoomIn/Out here
}

and in the Xaml

<StackPanel>
<ScrollViewer>
<local:CustomCanvas MouseWheel="UIElement_OnMouseWheel">
</local:CustomCanvas>
</ScrollViewer>
</StackPanel>

How to disable ScrollViewer in ListBox?

You can remove the ScrollViewer from a ListBox by changing its control template to something much simpler:

<ListBox>
<ListBox.Template>
<ControlTemplate>
<ItemsPresenter />
</ControlTemplate>
</ListBox.Template>
...
</ListBox>

However, I question the value of nesting ListBoxes. Remember that each ListBox is a Selector and has a concept of which item is "selected". Does it really make sense to have a selected item inside a selected item, inside a selected item?

I would suggest changing the "inner" ListBoxes to simple ItemsControls so that the nested lists can't have selected items. That would make for a much simpler user experience. You may still need to retemplate the inner ItemsControls in the same way to remove the scrollbars, but at least the user won't get confused about which item is "selected".

How to disable scroll when I click on one child element

When a partially visible element in a ScrollViewer is focused, the RequestBringIntoView event is raised, requesting that the parent ScrollViewer should scroll the whole element extent into view. However, often this is not the desired behavior as it causes this "jerking" scrolling. The easiest way to prevent is to handle the event before it bubbles to the parent ScrollViewer.

 <ScrollViewer>
<ItemsControl x:Name="ic" RequestBringIntoView="OnItemsControlRequestBringIntoView">
. . .
</ItemsControl>
</ScrollViewer>

private void OnListBoxRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
e.Handled = true;
}

Child elements of scrollviewer preventing scrolling with mouse wheel?

Specifying a ControlTemplate for the Listbox which doesn't include a ScrollViewer solves the problem. See this answer and these two MSDN pages for more information:

ControlTemplate

ListBox Styles and Templates

WPF: Help with Scrollviewer MouseWheel Behavior

I figured out the problem. I was actually using a custom WrapPanel that i created, and i forgot that control had its own scrollviewer (invisble since it was set to Auto size). So the wrappanel's scroller was stealing the mousewheel event from the outside scroller. I took out the scroller in the custom wrap panel and everything works normal now.

Avoid UserControl to catch mouse wheel scrolling

The problem is the ListBox has its own ScrollViewer. Copy the template for the ListBox and remove the embedded ScrollViewer.

Here's a complete example with the embedded ScrollViewer commented out:

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="ListBorder" Color="#828790"/>
<Style x:Key="ListBoxStyleNoScrollViewer" TargetType="{x:Type ListBox}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true">
<!--<ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">-->
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<!--</ScrollViewer>-->
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<ScrollViewer>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition Height="100"/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>

<ListBox Grid.Row="1" Style="{StaticResource ListBoxStyleNoScrollViewer}" >
<ListBox.Items>
<ListBoxItem>One</ListBoxItem>
<ListBoxItem>Two</ListBoxItem>
<ListBoxItem>Three</ListBoxItem>
<ListBoxItem>Four</ListBoxItem>
</ListBox.Items>
</ListBox>
</Grid>
</ScrollViewer>
</Window>

If the list box has Style="{StaticResource ListBoxStyleNoScrollViewer}" then the scroll wheel works when over the list box. If not then the sample exhibits the problem you mention.



Related Topics



Leave a reply



Submit