I have created a custom Circle control and added in to a canvas control like following code: (Note:- The main form has a button control. It will create a circle control inside Canvas control dynamically.)
_panel.Children.Add(_circle);
Now I need to implement drag and move functionality inside the Canvas control. I have tried to implement using the following code:
Circle.xmal.cs
protected void shape_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
xMouseLeftButtonDown(sender, e);
}
protected void shape_MouseMove(object sender, MouseEventArgs e)
{
xMouseMove(sender, e);
}
protected void shape_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
xMouseLeftButtonUp(sender, e);
}
Main.xaml.cs
protected void shape_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
source = (UIElement)sender;
Mouse.Capture(source);
captured = true;
x_shape = Canvas.GetLeft(source);
x_canvas = e.GetPosition(LayoutRoot).X;
y_shape = Canvas.GetTop(source);
y_canvas = e.GetPosition(LayoutRoot).Y;
}
protected void shape_MouseMove(object sender, MouseEventArgs e)
{
if (captured)
{
double x = e.GetPosition(LayoutRoot).X;
double y = e.GetPosition(LayoutRoot).Y;
x_shape += x - x_canvas;
Canvas.SetLeft(source, x_shape);
x_canvas = x;
y_shape += y - y_canvas;
Canvas.SetTop(source, y_shape);
y_canvas = y;
}
}
protected void shape_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Mouse.Capture(null);
captured = false;
}
But its not working. can anyone suggest me how to implement this.