How can I move shape created by a button on the canvas in WPF?
<p>I am very new to C# and WPF and would to create a WPF application that draws shapes with a button. The shapes then need to be able to move around the canvas. When I create a shape in the XAML it moves. However I cannot get the one created by the button to move. Could anyone please assist? Below are the XAML and code that i am using.</p>

<p>XAML:</p>

<pre><code>&lt;Window x:Class="All_test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"&gt;
&lt;Canvas x:Name="canvas" &gt;
    &lt;Button Content="Button" Canvas.Left="250" Canvas.Top="260" Width="75" Click="Button_Click_1" /&gt;
    &lt;Rectangle x:Name="rect" 
               Height="100" Width ="100" Fill="red"
               MouseLeftButtonDown="rect_MouseLeftButtonDown" 
            MouseLeftButtonUp="rect_MouseLeftButtonUp"
            MouseMove="rect_MouseMove" 
               Canvas.Left="342" Canvas.Top="110" /&gt;
&lt;/Canvas&gt;
</code></pre>

<p></p>

<p>This is the code I am using to move the red square drawn in XAML. How can I do the same for the green one created by the button?</p>

<pre><code>public partial class MainWindow : Window
{
    private bool _isRectDragInProg;

    public MainWindow()
    {
        InitializeComponent();
    }


    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Rectangle rect = new Rectangle();
        rect.Fill = new SolidColorBrush(Colors.Green);
        rect.Stroke = new SolidColorBrush(Colors.Black);
        rect.Height = 100;
        rect.Width = 100;
        rect.StrokeThickness = 4;
        canvas.Children.Add(rect);
        InitializeComponent();

    }


    private void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _isRectDragInProg = true;
        rect.CaptureMouse();
    }


    private void rect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        _isRectDragInProg = false;
        rect.ReleaseMouseCapture();
    }


    private void rect_MouseMove(object sender, MouseEventArgs e)
    {
        if (!_isRectDragInProg) return;

        // get the position of the mouse relative to the Canvas
        var mousePos = e.GetPosition(canvas);

        // center the rect on the mouse
        double left = mousePos.X - (rect.ActualWidth / 2);
        double top = mousePos.Y - (rect.ActualHeight / 2);
        Canvas.SetLeft(rect, left);
        Canvas.SetTop(rect, top);
    }
</code></pre>