如何侦听 ItemsControl 中两个项目之间的边距中的鼠标滚轮事件?

发布于 2024-11-26 02:08:16 字数 1047 浏览 1 评论 0原文

我有一个看起来像这样的 ItemsControl

<ItemsControl ItemsSource="{Binding}" PreviewMouseWheel="ItemsControl_PreviewMouseWheel" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Margin="0,4"><!--Margin to keep the items from being smashed too closely together-->
                ...
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

ItemsControl 将是可滚动区域中的几个项目之一,但是如果我在 ItemsControl 上使用鼠标滚轮,什么也没有发生。所以我想将鼠标滚轮事件进一步转发到树上:

private void ItemsControl_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    e.Handled = true;

    RaiseEvent(new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
    {
        RoutedEvent = UIElement.MouseWheelEvent
    });
}

这基本上有效。但是,当鼠标滚动到控件中两个项目之间的 Margin 区域(由 Margin="0,4" 指定)时,没有任何反应。我尝试将网格放入装饰器中,例如 Border,但这似乎没有帮助。如何捕获这些鼠标滚轮事件并将它们转发到树上?

I have an ItemsControl that looks something like this:

<ItemsControl ItemsSource="{Binding}" PreviewMouseWheel="ItemsControl_PreviewMouseWheel" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Margin="0,4"><!--Margin to keep the items from being smashed too closely together-->
                ...
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

The ItemsControl will be one of several items in a scrollable area, but if I use the mouse wheel over the ItemsControl, nothing happens. So I want to forward the mouse wheel events further up the tree:

private void ItemsControl_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    e.Handled = true;

    RaiseEvent(new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
    {
        RoutedEvent = UIElement.MouseWheelEvent
    });
}

This mostly works. But when the mouse scrolls over the Margin area (specified by Margin="0,4") between two items in the control, nothing happens. I tried putting the grid inside a decorator, such as a Border, but that doesn't seem to help. How can I capture these mouse wheel events, and forward them up the tree?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

止于盛夏 2024-12-03 02:08:16

边距是控件周围剩余的空间区域,并且由于它只是空白空间,因此它不处理任何事件。

另一种方法是嵌套控件,例如将带有边距的 Grid 放入没有边距的 DockPanel 中,或者使用具有 Padding 属性(例如 Border)的 WPF 控件

The Margin is the area of space left around a Control, and since it's just empty space it doesn't process any events.

An alternative is to either nest controls, such as putting your Grid with the Margin inside a DockPanel without a Margin, or to use a WPF control that has the Padding property such as Border

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文