一种获取“按钮按下事件”的方法对于 WPF ScrollViewer 控件

发布于 2024-10-16 06:42:08 字数 354 浏览 3 评论 0原文

我的自定义控件使用 ScrollViewer,我想确定用户何时按下水平滚动增量按钮,而控件已经滚动到水平最大值。

查看器包含一个画布,其想法是,如果用户尝试使用按钮滚动超过其范围,画布将扩展。

ScrollViewer 似乎没有任何与按钮具体相关的事件,并且 ScrollChanged 本身没有用,因为当栏处于其范围时它不会触发。

.Net Reflector 并没有产生太多的用途。我能得到的最接近的是为 mouseLeftButtonDown 添加一个事件处理程序,我可以看到查看器状态,但不知道如何确定鼠标事件是否源自按钮。

任何人都可以想出一种方法来做到这一点吗?

My custom control uses a ScrollViewer and I want to determine when the user presses the horizontal scroll increment button while the control is already scrolled to the horizontal max.

The viewer contains a canvas and the idea is the canvas will extend if the user tries to scroll past its extent with the buttons.

ScrollViewer doesn't appear to have any events which relate to the buttons specifically and ScrollChanged is no use on its own since it doesn't trigger when the bar is at its extent.

.Net Reflector hasn't yielded much of use. The closest I can get is adding an event handler for mouseLeftButtonDown, I can see the viewers state but not how to determine whether the mouse event originated from the button.

Can anyone think of a way to do this?

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

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

发布评论

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

评论(1

风苍溪 2024-10-23 06:42:08

您可以尝试通过 VisualTree 获取控件的按钮,并将处理程序附加到它们的单击事件。

编辑:我编写了一个扩展方法,用于通过路径获取可视化树的项目:

public static class ExtensionMethods
{
    public static DependencyObject GetVisualChildFromTreePath(this DependencyObject dpo, int[] path)
    {
        if (path.Length == 0) return dpo;
        List<int> newPath = new List<int>(path);
        newPath.RemoveAt(0);
        return VisualTreeHelper.GetChild(dpo, path[0]).GetVisualChildFromTreePath(newPath.ToArray());
    }
}

如果您的 ScrollViewer 被称为 sv 您应该能够获得如下按钮:

RepeatButton button1 = sv.GetVisualChildFromTreePath(new int[] { 0, 2, 0, 0 }) as RepeatButton; //Up
RepeatButton button2 = sv.GetVisualChildFromTreePath(new int[] { 0, 2, 0, 2 }) as RepeatButton; //Down
RepeatButton button3 = sv.GetVisualChildFromTreePath(new int[] { 0, 3, 0, 0 }) as RepeatButton; //Left
RepeatButton button4 = sv.GetVisualChildFromTreePath(new int[] { 0, 3, 0, 2 }) as RepeatButton; //Right

注意:按钮仅在相应的滚动条时存在已启用。通过使用其他数据类型,扩展方法可能会提高性能。

You could try to get the buttons of the control via the VisualTree and attach a handler to their click events.

Edit: I wrote an extension method for getting items of visual tree via a path:

public static class ExtensionMethods
{
    public static DependencyObject GetVisualChildFromTreePath(this DependencyObject dpo, int[] path)
    {
        if (path.Length == 0) return dpo;
        List<int> newPath = new List<int>(path);
        newPath.RemoveAt(0);
        return VisualTreeHelper.GetChild(dpo, path[0]).GetVisualChildFromTreePath(newPath.ToArray());
    }
}

If your ScrollViewer is called sv you should be able to get the buttons like this:

RepeatButton button1 = sv.GetVisualChildFromTreePath(new int[] { 0, 2, 0, 0 }) as RepeatButton; //Up
RepeatButton button2 = sv.GetVisualChildFromTreePath(new int[] { 0, 2, 0, 2 }) as RepeatButton; //Down
RepeatButton button3 = sv.GetVisualChildFromTreePath(new int[] { 0, 3, 0, 0 }) as RepeatButton; //Left
RepeatButton button4 = sv.GetVisualChildFromTreePath(new int[] { 0, 3, 0, 2 }) as RepeatButton; //Right

Note: Buttons only exist if the respective scollbar is enabled. The extension method could probably improved in terms of performance by using other datatypes.

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