PreviewMouseMove中的面板无法正常工作 [英] Panel in PreviewMouseMove not working properly
本文介绍了PreviewMouseMove中的面板无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好专家,
我正在使用WPF和C#.在此,我正在使用图像上的面板数.并尝试使用e.LeftButton == MouseButtonState.Pressed在图像上移动面板.它可以正确移动,但是当一个面板在另一面板上覆盖时,PreviewMouseMove会保留较旧的面板,然后拾取新的面板.
使用此代码
Hello Experts,
I am working with WPF and C#. In this I am using number of panels on image. and trying to move panels over an image using e.LeftButton == MouseButtonState.Pressed.Its moving properly but when one panel override on another panel PreviewMouseMove leaves older one and pick up new one.
Using this code
void panel_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point x = e.GetPosition(ImageHolder);
StackPanel panel = (StackPanel)sender;
if (e.LeftButton == MouseButtonState.Pressed)
{
//restrict dropped panel image to be in image area
if ((Canvas.GetLeft(panel) + (x.X - p.X)) <= (Img.Width - panel.Width) && (Canvas.GetLeft(panel) + (x.X - p.X)) >= 0)
{
Canvas.SetLeft(panel, Canvas.GetLeft(panel) + (x.X - p.X));
}
if ((Canvas.GetTop(panel) + (x.Y - p.Y)) <= (Img.Height - panel.Height) && (Canvas.GetTop(panel) + (x.Y - p.Y)) >= 0)
{
Canvas.SetTop(panel, Canvas.GetTop(panel) + (x.Y - p.Y));
}
}
p = x;
}
向我建议一些修改或其他选择.
谢谢
Suggest me some modifications or else any alternative.
Thanks
推荐答案
我误解了OP正在使用WPF,因此该解决方案不适用于他/她. /> PreviewMouseMove无法正常工作 [
I misunderstood that the OP was using WPF so this solution did not work for him/her see the link below for the solution that did.
PreviewMouseMove is not working[^]
Use a boolean to check if you are already dragging a panel. i.e.
bool dragging = false;
void panel_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && !dragging)
{
dragging = true;
//your code
}
else if (e.LeftButton == MouseButtonState.Released)
{
dragging = false;
}
}
像这样的东西应该可以正常工作.
something like this should work fine.
这篇关于PreviewMouseMove中的面板无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文