Skip to content

Instantly share code, notes, and snippets.

@punker76
Created September 18, 2018 13:25
Show Gist options
  • Save punker76/9fdd6531a9fa01b5f5e9dfaa15a7893b to your computer and use it in GitHub Desktop.
Save punker76/9fdd6531a9fa01b5f5e9dfaa15a7893b to your computer and use it in GitHub Desktop.
BubbleScrollEvent
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
namespace ExtraBehaviors
{
/// <summary>
/// The BubbleScrollEvent behavior can be used to prevent the mousewheel scrolling on a scrollable control.
/// The event will be bubble up to the parent control.
/// This behavior can be prevent with the left Shift key.
/// </summary>
public class BubbleScrollEvent : Behavior<UIElement>
{
protected override void OnAttached() {
base.OnAttached();
this.AssociatedObject.PreviewMouseWheel -= this.AssociatedObject_PreviewMouseWheel;
this.AssociatedObject.PreviewMouseWheel += this.AssociatedObject_PreviewMouseWheel;
}
protected override void OnDetaching() {
this.AssociatedObject.PreviewMouseWheel -= this.AssociatedObject_PreviewMouseWheel;
base.OnDetaching();
}
void AssociatedObject_PreviewMouseWheel(object sender, MouseWheelEventArgs e) {
if (!Keyboard.IsKeyDown(Key.LeftShift)) {
e.Handled = true;
var e2 = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta) {RoutedEvent = UIElement.MouseWheelEvent};
this.AssociatedObject.RaiseEvent(e2);
}
}
}
}
<ScrollViewer xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<Grid>
<i:Interaction.Behaviors>
<behaviors:BubbleScrollEvent />
</i:Interaction.Behaviors>
<!-- inner ScrollViewer -->
<ScrollViewer>
</ScrollViewer>
</Grid>
</ScrollViewer>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment