title: Add rectangle to Canvas using MVVM
body: <p>How do i add Rectangle to my View using MVVM?</p>
  
  <p>This is the code for my view.</p>
  
  <pre><code>&lt;Grid&gt;
              &lt;Image  x:Name="img"  Source="{Binding ImagePath, Source={x:Static vm:DrawingVM.instance}, Converter={StaticResource nullImageConverter}}" Stretch="None" &gt;
              &lt;/Image&gt;
  
              &lt;ItemsControl ItemsSource="{Binding ListRectangle, Source={x:Static vm:DrawingVM.instance}}" &gt;
  
                  &lt;ItemsControl.ItemsPanel&gt;
                      &lt;ItemsPanelTemplate&gt;
                          &lt;Canvas Background="Transparent"  x:Name="cnvas" Width="{Binding ElementName=img, Path=ActualWidth}" 
                          Height="{Binding ElementName=img,Path=ActualHeight}"
                          LayoutTransform="{Binding ElementName=img, Path=LayoutTransform}" &gt;
                              &lt;i:Interaction.Triggers&gt;
                                  &lt;i:EventTrigger EventName="MouseDown"&gt;
                                      &lt;!--&lt;command:EventToCommand CommandParameter="{Binding ElementName=cnvas}" Command="{Binding MouseDownCommand, Source={x:Static vm:DrawingVM.instance}}" PassEventArgsToCommand="True" /&gt;--&gt;
                                      &lt;ei:CallMethodAction MethodName="MouseDownEvente" TargetObject="{Binding Source={x:Static vm:DrawingVM.instance}}" /&gt;
                                  &lt;/i:EventTrigger&gt;
  
                              &lt;/i:Interaction.Triggers&gt;
                          &lt;/Canvas&gt;
                      &lt;/ItemsPanelTemplate&gt;
                  &lt;/ItemsControl.ItemsPanel&gt;
                  &lt;ItemsControl.ItemContainerStyle&gt;
                      &lt;Style TargetType="ContentPresenter"&gt;
                          &lt;Setter Property="Canvas.Left" Value="{Binding X}"/&gt;
                          &lt;Setter Property="Canvas.Top" Value="{Binding Y}"/&gt;
                      &lt;/Style&gt;
                  &lt;/ItemsControl.ItemContainerStyle&gt;
                  &lt;ItemsControl.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;Rectangle Width="{Binding Width}" Height="{Binding Height}" Stroke="Blue"  Fill="Transparent" /&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ItemsControl.ItemTemplate&gt;
              &lt;/ItemsControl&gt;
          &lt;/Grid&gt;
  </code></pre>
  
  <p>and this is my View Model</p>
  
  <pre><code>Canvas canvas = new Canvas();
          public void MouseDownEvente(object s, MouseButtonEventArgs e)
          {
              try
              {
                  if (s == null) return;
                  canvas = s as Canvas;
                  if (canvas == null) return;
  
                  startPoint = e.GetPosition(canvas);
  
                  // Remove the drawn rectanglke if any.
                  // At a time only one rectangle should be there
                  //if (rectSelectArea != null)
                  //    canvas.Children.Remove(rectSelectArea);
  
                  // Initialize the rectangle.
                  // Set border color and width
                  rectSelectArea = new Rectangle
                  {
                      Stroke = Brushes.Blue,
                      StrokeThickness = 2,
                      Fill = Brushes.Transparent,
                  };
  
                  Canvas.SetLeft(rectSelectArea, startPoint.X);
                  Canvas.SetTop(rectSelectArea, startPoint.X);
                  canvas.Children.Add(rectSelectArea);
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
                  Console.WriteLine(ex.StackTrace);
                  throw ex;
              }
          }
  </code></pre>
  
  <p>But its throwing an error:</p>
  
  <pre><code>Cannot explicitly modify Children collection of Panel used as ItemsPanel for ItemsControl. ItemsControl generates child elements for Panel.
  </code></pre>
  
  <p>So how do i solve this?</p>
  
  <p>I tried searching the same problem with mine. And used the solution that worked to them. But the error still persist. Can someone help me. Thank you.</p>