在 ListView 上用鼠标激活水平滚动

jac*_*k11 3 wpf mouse listview scroll

我有一个自定义的水平 ListView,它的模板中带有自定义的 ScrollViewer(使用 Blend 创建)。我希望它在使用鼠标滚轮时水平滚动。我怎样才能做到这一点?

Cha*_*lie 5

这应该通过一个Behavior更大的可重用性来完成。此外,ZSH 的逻辑是多余的,可以简化。这是我的代码:

/// <summary>
/// Allows an <see cref="ItemsControl"/> to scroll horizontally by listening to the
/// <see cref="PreviewMouseWheel"/> event of its internal <see cref="ScrollViewer"/>.
/// </summary>
public class HorizontalScrollBehavior : Behavior<ItemsControl>
{
    /// <summary>
    /// A reference to the internal ScrollViewer.
    /// </summary>
    private ScrollViewer ScrollViewer { get; set; }

    /// <summary>
    /// By default, scrolling down on the wheel translates to right, and up to left.
    /// Set this to true to invert that translation.
    /// </summary>
    public bool IsInverted { get; set; }

    /// <summary>
    /// The ScrollViewer is not available in the visual tree until the control is loaded.
    /// </summary>
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        AssociatedObject.Loaded -= OnLoaded;

        ScrollViewer = VisualTreeHelpers.FindVisualChild<ScrollViewer>(AssociatedObject);

        if (ScrollViewer != null)
        {
            ScrollViewer.PreviewMouseWheel += OnPreviewMouseWheel;
        }
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        if (ScrollViewer != null)
        {
            ScrollViewer.PreviewMouseWheel -= OnPreviewMouseWheel;
        }
    }

    private void OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        var newOffset = IsInverted ?
            ScrollViewer.HorizontalOffset + e.Delta :
            ScrollViewer.HorizontalOffset - e.Delta;

        ScrollViewer.ScrollToHorizontalOffset(newOffset);
    }
}
Run Code Online (Sandbox Code Playgroud)

您需要添加以下引用: System.WindowsSystem.Windows.ControlsSystem.Windows.Input,并且您可能需要获取 Blend SDK NuGet 包,并System.Windows.Interactivity在程序集扩展部分中查找和引用DLL。

将此用于VisualTreeHelpers

public class VisualTreeHelpers
{
    /// <summary>
    /// Return the first visual child of element by type.
    /// </summary>
    /// <typeparam name="T">The type of the Child</typeparam>
    /// <param name="obj">The parent Element</param>
    public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child is T)
                return (T)child;
            else
            {
                T childOfChild = FindVisualChild<T>(child);
                if (childOfChild != null)
                    return childOfChild;
            }
        }
        return null;
     }
}
Run Code Online (Sandbox Code Playgroud)

参考:https : //codereview.stackexchange.com/questions/44760/is-there-a-better-way-to-get-a-child

请注意,这是不一样的VisualTreeHelperWindows.System.Media

以下是在 XAML 中使用它的方法:

<ListBox>
    <i:Interaction.Behaviors>
        <behaviors:HorizontalScrollBehavior />
    </i:Interaction.Behaviors>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

i命名空间被声明为xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

behaviors 被声明为

xmlns:behaviors="clr-namespace:MyNamespace"

MyNamespace包含HorizontalScrollBehavior类的命名空间在哪里。