Click here to Skip to main content
15,569,696 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:
C#
void panel_PreviewMouseMove(object sender, MouseEventArgs e)
{
    Point x = e.GetPosition(ImageHolder);
    StackPanel panel = (StackPanel)sender;
    if (e.LeftButton == MouseButtonState.Pressed && !dragging)
    {
        dragging = true;
               
        //restrict dropped camera image to be in map 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));
        }
    }
    else if (e.LeftButton == MouseButtonState.Released)
    {
        dragging = false;             
    }

    p = x; 
}


Suggest me some modifications or else any alternative.

Thanks
Posted
Updated 24-Nov-11 21:18pm
v3
Comments
LanFanNinja 25-Nov-11 2:20am    
So I take it the my past solution did not work out? Sorry!
Is there any way you can post all of the code for your class so that I can run it and try to find a solution for you?
KIDYA 25-Nov-11 2:31am    
Yes your past solution not worked..I tried many possibilities on it but not succeed.I am not able to post class over here.But I guess u got my problem? I want to just move panel over ImageHolder(not a drag and drop on it) and in above code I am setting imageHolder boundary for panel.
LanFanNinja 25-Nov-11 4:35am    
Check my solution below. Hopefully this time it will work for you. Let me know.
And again I am sorry my mind has not been working so well lately :)

1 solution

C#
StackPanel panel;

void panel_PreviewMouseMove(object sender, MouseEventArgs e)
{
    Point x = e.GetPosition(ImageHolder);

    if (e.LeftButton == MouseButtonState.Pressed)
    {
        if (panel == null)
        {
            panel = (StackPanel)sender;
        }

        //restrict dropped camera image to be in map 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));
        }
    }
    else if (e.LeftButton == MouseButtonState.Released)
    {
        panel = null;
    }

    p = x;
}
 
Share this answer
 
Comments
KIDYA 25-Nov-11 5:13am    
Hey LanFanNinja, It works this time.Thanks alot :)
LanFanNinja 25-Nov-11 5:16am    
You're welcome. Sorry it took so long.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900