在画布中移动按钮 [英] Moving buttons in a Canvas

查看:86
本文介绍了在画布中移动按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码应该在鼠标悬停在画布上并用户按下Ctrl时将UIElements在画布中移动。

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

void mousemove(object sender,MouseEventArgs e)
{
Point p = e.GetPosition(canvas);
if(拖动)
{
if(elem == null)返回;
//Canvas.SetLeft(myButton,p.X); <-可行,但是为什么我将其概括化却不可行?
Canvas.SetLeft(elem,p.X);
Canvas.SetTop(elem,p.Y);
}
}

任何形状组件,例如当我将鼠标悬停在矩形上并单击控件时,矩形将移动。但是它不适用于Buttons,TextBoxes,TextViews等。有人可以解释吗?

解决方案

Mouse.DirectlyOver 说:


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


换句话说:Button由几个子元素组成,例如ButtonChrome和TextBlock(通常不是TextBox,我认为这是MSDN页面上的错字)。当您调用 Mouse.DirectlyOver 时,您可能会得到其中一个元素,而不是Button。



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



您可能想走上视觉树(使用 VisualTreeHelper.GetParent ),直到找到您感兴趣的拖动内容。如何确定自己是否对任何给定元素感兴趣?您可以一直进行下去,直到找到画布的父项,或者直到找到某种给定类型的事物为止(当找到从Control派生的事物时停下来可能是一个不错的起点)。 / p>

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?

解决方案

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

这篇关于在画布中移动按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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