在画布中移动按钮
当鼠标悬停在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
的文档
Mouse.DirectlyOver
说:换句话说:Button 由多个子元素组成,例如 ButtonChrome 和 TextBlock(通常不是 TextBox——我认为这是 MSDN 页面上的拼写错误)。当您调用
Mouse.DirectlyOver
时,您可能会获取这些元素之一,而不是 Button。由于这些元素不是 Canvas 的父级(它们是 Button 控件模板中某些内容的父级,很可能是 Grid),因此设置 Canvas.Left 和 Canvas.Top 附加属性将不起作用。
您可能想沿着可视化树向上走(使用 VisualTreeHelper.GetParent),直到找到您有兴趣拖动的内容。如何确定您是否对任何给定元素感兴趣取决于您。您可以继续下去,直到找到与您的画布相关的内容,或者您可以继续下去,直到找到属于给定类型集之一的内容(当您发现从控制派生的内容时停止可能是一个不错的起点)。
The documentation for
Mouse.DirectlyOver
says: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).
我的猜测是,这些事件被不起作用的控件的实现所吞噬。
我现在不在电脑前,但是不是有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.