手动在 ScrollViewer 中触发 MouseDown 事件 WPF

发布于 2024-11-28 06:53:27 字数 1501 浏览 0 评论 0原文

我使用 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 技术交流群。

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

发布评论

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

评论(1

唱一曲作罢 2024-12-05 06:53:27

我不确定它是否真的有效,但您需要将: 更改

FrameworkElement el = lastPreviewMouseDownMouseButtonEventArgs.Source as FrameworkElement;

为:

FrameworkElement el = lastPreviewMouseDownMouseButtonEventArgs.OriginalSource as FrameworkElement;

您可能还需要引发 PreviewMouseButtonDownMouseButtonDown 事件。再次强调,不确定这样做是否真的能达到你想要的效果。

相反,为什么不直接获取被单击的 TabItem,并设置 IsSelected = true 呢?

I'm not sure it will actually work, but you will need to change:

FrameworkElement el = lastPreviewMouseDownMouseButtonEventArgs.Source as FrameworkElement;

to:

FrameworkElement el = lastPreviewMouseDownMouseButtonEventArgs.OriginalSource as FrameworkElement;

You will probably also need to raise both the PreviewMouseButtonDown and the MouseButtonDown 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?

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