gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:26:16 26 4
gpt4 key购买 nike

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

最佳答案

这应该通过行为来完成,以获得更好的可重用性。另外,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);
}
}

您需要添加以下引用: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;
}
}

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

请注意,它与 Windows.System.Media 中的 VisualTreeHelper 不同。

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

<ListBox>
<i:Interaction.Behaviors>
<behaviors:HorizontalScrollBehavior />
</i:Interaction.Behaviors>
</ListBox>

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

行为声明为

xmlns:behaviors="clr-namespace:MyNamespace"

其中 MyNamespace 是包含 Horizo​​ntalScrollBehavior 类的命名空间。

关于wpf - 在 ListView 上用鼠标激活水平滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12069407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com