手动在 ScrollViewer 中触发 MouseDown 事件 WPF
我使用 TabControl ->滚动查看器 ->与 pMouseLeftButtonDown 处理程序的边框...我的目标是实现拖动滚动滚动查看器... 我有一个自定义滚动查看器,我取消所有previewmouseleftbuttondown事件(模板内边框上的处理程序):
private void pMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!block)
{
block = true;
lastPreviewMouseDownMouseButtonEventArgs = null;
return;
}
else
{
block = true;
}
lastPreviewMouseDownMouseButtonEventArgs = null;
mouseDragStartPoint = e.GetPosition(this);
scrollStartOffset.X = HorizontalOffset;
scrollStartOffset.Y = VerticalOffset;
e.Handled = true;
lastPreviewMouseDownMouseButtonEventArgs = e;
}
在某些特殊情况下,我想手动触发“lastPreviewMouseDownMouseButtonEventArgs”。这似乎有效,因为在 raiseEvent() 调用之后,方法 pMouseLeftButtonDown() 被再次调用,并且这个新事件不会被阻止(handled = true)。问题是 ui 元素没有获取鼠标事件 =>没有按下鼠标左键,这意味着没有选项卡选择...
问题出在哪里?
FrameworkElement el = lastPreviewMouseDownMouseButtonEventArgs.Source as FrameworkElement;
lastPreviewMouseDownMouseButtonEventArgs.Handled = false;
Debug.WriteLine(el);
MouseButtonEventArgs newEvent = new MouseButtonEventArgs(Mouse.PrimaryDevice, Environment.TickCount, MouseButton.Left) { RoutedEvent = FrameworkElement.MouseLeftButtonDownEvent };
el.RaiseEvent(lastPreviewMouseDownMouseButtonEventArgs);
I use a TabControl -> ScrollViewer -> Border with pMouseLeftButtonDown handler.... My goal is to implement a drag to scroll scrollviewer...
i have a custom scrollviewer and i cancel all previewmouseleftbuttondown events (Handler on a border inside the template):
private void pMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!block)
{
block = true;
lastPreviewMouseDownMouseButtonEventArgs = null;
return;
}
else
{
block = true;
}
lastPreviewMouseDownMouseButtonEventArgs = null;
mouseDragStartPoint = e.GetPosition(this);
scrollStartOffset.X = HorizontalOffset;
scrollStartOffset.Y = VerticalOffset;
e.Handled = true;
lastPreviewMouseDownMouseButtonEventArgs = e;
}
In some special cases i would like to fire "lastPreviewMouseDownMouseButtonEventArgs" manually. This seems to work because after the raiseEvent() call the method pMouseLeftButtonDown() is called again and this new event won't be blocked (handled = true). The problem is that the ui element gets no mouse event => no mouse left button down, which means no tab selection...
Where is the problem?
FrameworkElement el = lastPreviewMouseDownMouseButtonEventArgs.Source as FrameworkElement;
lastPreviewMouseDownMouseButtonEventArgs.Handled = false;
Debug.WriteLine(el);
MouseButtonEventArgs newEvent = new MouseButtonEventArgs(Mouse.PrimaryDevice, Environment.TickCount, MouseButton.Left) { RoutedEvent = FrameworkElement.MouseLeftButtonDownEvent };
el.RaiseEvent(lastPreviewMouseDownMouseButtonEventArgs);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定它是否真的有效,但您需要将: 更改
为:
您可能还需要引发
PreviewMouseButtonDown
和MouseButtonDown
事件。再次强调,不确定这样做是否真的能达到你想要的效果。相反,为什么不直接获取被单击的 TabItem,并设置
IsSelected = true
呢?I'm not sure it will actually work, but you will need to change:
to:
You will probably also need to raise both the
PreviewMouseButtonDown
and theMouseButtonDown
events. Again, not sure if doing this will actually work for what you want.Instead, why don't you just get the TabItem that was clicked, and set
IsSelected = true
?