一种获得“按钮按下事件"的方法是在网络上获得一个“按钮按下事件".用于WPF ScrollViewer控件 [英] A way to get a "button press event" for a WPF ScrollViewer control

查看:83
本文介绍了一种获得“按钮按下事件"的方法是在网络上获得一个“按钮按下事件".用于WPF ScrollViewer控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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似乎没有任何与按钮有关的事件,并且ScrollChanged本身没有用,因为它不会在条形图处于其范围内时触发.

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的使用率不高.我能得到的最接近的结果是为mouseLeftButtonDown添加了一个事件处理程序,我可以看到查看器的状态,但是看不到如何确定鼠标事件是否起源于按钮.

.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?

推荐答案

您可以尝试通过VisualTree获取控件的按钮,并将处理程序附加到其click事件.

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

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

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());
    }
}

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

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.

这篇关于一种获得“按钮按下事件"的方法是在网络上获得一个“按钮按下事件".用于WPF ScrollViewer控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆