如何侦听 ItemsControl 中两个项目之间的边距中的鼠标滚轮事件?
我有一个看起来像这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
边距是控件周围剩余的空间区域,并且由于它只是空白空间,因此它不处理任何事件。
另一种方法是嵌套控件,例如将带有边距的 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