在画布中移动按钮

发布于 2024-12-10 17:40:35 字数 1030 浏览 0 评论 0原文

当鼠标悬停在 UIElement 上并且用户按下 Ctrl 键时,以下代码应该在画布中移动 UIElement。

void keydown(Object sender, KeyEventArgs e)
        {
            if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
            {
                control++;
                if (control == 1)
                {
                    drag = true;
                    elem = (UIElement)Mouse.DirectlyOver;
                }
                else
                    drag = false;
                control %= 2;
            }
        }

        void mousemove(object sender, MouseEventArgs e)
        {
            Point p = e.GetPosition(canvas);
            if (drag)
            {
                if (elem == null) return;
                //Canvas.SetLeft(myButton, p.X);  <-- this works, but then why doesn't it work when I generalize it?
                Canvas.SetLeft(elem, p.X);
                Canvas.SetTop(elem, p.Y);
            }
        }

任何 Shapes 组件,例如,当我将鼠标悬停在矩形上并点击控件时,矩形都会移动。但它不适用于按钮、文本框、文本视图等。任何人都可以解释吗?

The following code is supposed to move UIElements in a canvas, when the mouse hovers over them, and the user hits Ctrl.

void keydown(Object sender, KeyEventArgs e)
        {
            if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
            {
                control++;
                if (control == 1)
                {
                    drag = true;
                    elem = (UIElement)Mouse.DirectlyOver;
                }
                else
                    drag = false;
                control %= 2;
            }
        }

        void mousemove(object sender, MouseEventArgs e)
        {
            Point p = e.GetPosition(canvas);
            if (drag)
            {
                if (elem == null) return;
                //Canvas.SetLeft(myButton, p.X);  <-- this works, but then why doesn't it work when I generalize it?
                Canvas.SetLeft(elem, p.X);
                Canvas.SetTop(elem, p.Y);
            }
        }

Any Shapes component, e.g. Rectangles will move when I hover the mouse over them, and hit control..But it doesn't work with Buttons, TextBoxes, TextViews, etc. Can anyone explain?

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

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

发布评论

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

评论(2

美男兮 2024-12-17 17:40:35

的文档Mouse.DirectlyOver 说:

控件可以由多个元素组成。 DirectlyOver 报告鼠标指针位于复合控件中的特定元素,而不是控件本身。例如,根据指针位于 Button 的哪一部分,DirectlyOver 属性可以报告 Content 属性的 TextBox 或 ButtonChrome。

换句话说:Button 由多个子元素组成,例如 ButtonChrome 和 TextBlock(通常不是 TextBox——我认为这是 MSDN 页面上的拼写错误)。当您调用 Mouse.DirectlyOver 时,您可能会获取这些元素之一,而不是 Button。

由于这些元素不是 Canvas 的父级(它们是 Button 控件模板中某些内容的父级,很可能是 Grid),因此设置 Canvas.Left 和 Canvas.Top 附加属性将不起作用。

您可能想沿着可视化树向上走(使用 VisualTreeHelper.GetParent),直到找到您有兴趣拖动的内容。如何确定您是否对任何给定元素感兴趣取决于您。您可以继续下去,直到找到与您的画布相关的内容,或者您​​可以继续下去,直到找到属于给定类型集之一的内容(当您发现从控制派生的内容时停止可能是一个不错的起点)。

The documentation for Mouse.DirectlyOver says:

Controls can be composed of multiple elements. DirectlyOver reports the specific element in the composite control the mouse pointer is over and not the control itself. For example, depending on which part of a Button the pointer is over, the DirectlyOver property could report the TextBox of the Content property or the ButtonChrome.

In other words: A Button is made up of several sub-elements, like a ButtonChrome and a TextBlock (usually not a TextBox -- I think that's a typo on the MSDN page). When you call Mouse.DirectlyOver, you're probably getting one of those elements, rather than the Button.

Since those elements are not parented to a Canvas (they're parented to something in the Button's control template, most likely a Grid), setting the Canvas.Left and Canvas.Top attached properties will have no effect.

You probably want to walk up the visual tree (using VisualTreeHelper.GetParent) until you find something you're interested in dragging. How you determine whether you're interested in any given element is up to you. You could go until you find something that's parented to your Canvas, or you could just go until you find something that's one of a given set of types (stopping when you find something that descends from Control might be a decent place to start).

过期以后 2024-12-17 17:40:35

我的猜测是,这些事件被不起作用的控件的实现所吞噬。

我现在不在电脑前,但是不是有previewmousemove事件吗?尝试使用那个?

更新:从头开始!

只需使用 e.Souce 即可。事件源被设计为您可能感兴趣的元素。您需要转换为 UIElement,但我很确定这会起作用。

My guess is that those events are swallowed by the implementation of the controls that don't work.

I'm not in front of my computer now, but isn't there a previewmousemove event? Try using that?

Update: scratch that!

Just use e.Souce. The event source is designed to be the element your likely to be interested in. You'll need to cast to UIElement, but I'm pretty sure that'll work.

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