Skip to content

Instantly share code, notes, and snippets.

@cnbluefire
Last active April 25, 2025 10:06
Show Gist options
  • Select an option

  • Save cnbluefire/6378524bf99a76ed69c6587660250089 to your computer and use it in GitHub Desktop.

Select an option

Save cnbluefire/6378524bf99a76ed69c6587660250089 to your computer and use it in GitHub Desktop.
禁止使用鼠标滚轮操作 FlipView
public static class FlipViewHelper
{
public static bool GetAllowPointerWheelFlip(FlipView obj)
{
return (bool)obj.GetValue(AllowPointerWheelFlipProperty);
}
public static void SetAllowPointerWheelFlip(FlipView obj, bool value)
{
obj.SetValue(AllowPointerWheelFlipProperty, value);
}
public static readonly DependencyProperty AllowPointerWheelFlipProperty =
DependencyProperty.RegisterAttached("AllowPointerWheelFlip", typeof(bool), typeof(BlankPage2), new PropertyMetadata(true, (s, a) =>
{
if (s is FlipView sender && !Equals(a.NewValue, a.OldValue))
{
sender.Loaded -= FlipView_Loaded;
sender.Loaded += FlipView_Loaded;
UpdateIsPointerWheelEnabled(sender);
}
}));
private static void FlipView_Loaded(object sender, RoutedEventArgs e)
{
UpdateIsPointerWheelEnabled((FlipView)sender);
}
private static void UpdateIsPointerWheelEnabled(FlipView flipView)
{
if (flipView == null) return;
if (VisualTreeHelper.GetChildrenCount(flipView) > 0)
{
var layoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(flipView, 0);
layoutRoot.PointerWheelChanged -= LayoutRoot_PointerWheelChanged;
layoutRoot.PointerWheelChanged += LayoutRoot_PointerWheelChanged;
}
}
private static void LayoutRoot_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
if (sender is FrameworkElement fe)
{
if (VisualTreeHelper.GetParent(fe) is FlipView flipView)
{
if (!GetAllowPointerWheelFlip(flipView))
{
e.Handled = true;
}
}
else
{
fe.PointerWheelChanged -= LayoutRoot_PointerWheelChanged;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment