title: Convert Code behind MouseButtonEventArgs into MVVM
body: <p>Im still new to MVVM but im trying to learn it.</p>
  
  <p>I need to resize the Drawn rectangle into my canvas. But i want to do it in MVVM way.</p>
  
  <p>I found this post and its working and it suite my needs but its not MVVM</p>
  
  <p><a href="https://denisvuyka.wordpress.com/2007/10/15/wpf-simple-adorner-usage-with-drag-and-resize-operations/" rel="nofollow noreferrer">https://denisvuyka.wordpress.com/2007/10/15/wpf-simple-adorner-usage-with-drag-and-resize-operations/</a></p>
  
  <p>I am trying to convert this code into MVVM</p>
  
  <pre><code>// Handler for element selection on the canvas providing resizing adorner
          void myCanvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
          {
              // Remove selection on clicking anywhere the window
              if (selected)
              {
                  selected = false;
                  if (selectedElement != null)
                  {
                      // Remove the adorner from the selected element
                      aLayer.Remove(aLayer.GetAdorners(selectedElement)[0]);                    
                      selectedElement = null;
                  }
              }
  
              // If any element except canvas is clicked, 
              // assign the selected element and add the adorner
              if (e.Source != myCanvas)
              {
                  _isDown = true;
                  _startPoint = e.GetPosition(myCanvas);
  
                  selectedElement = e.Source as UIElement;
  
                  _originalLeft = Canvas.GetLeft(selectedElement);
                  _originalTop = Canvas.GetTop(selectedElement);
  
                  aLayer = AdornerLayer.GetAdornerLayer(selectedElement);
                  aLayer.Add(new ResizingAdorner(selectedElement));
                  selected = true;
                  e.Handled = true;
              }
          }
  </code></pre>
  
  <p>I am using Devexpress MVVM</p>
  
  <p>and this is my xaml code</p>
  
  <pre><code> &lt;Canvas Name="myCanvas" Background="Gray"&gt;
          &lt;dxmvvm:Interaction.Behaviors&gt;
              &lt;dxmvvm:EventToCommand Command="{Binding OnSelectedCommand , Source={x:Static vm:Vm.instance}}" 
                                         EventName="MouseDown" CommandParameter="{Binding ElementName=myCanvas}"
                                         PassEventArgsToCommand="True"&gt;
              &lt;/dxmvvm:EventToCommand&gt;
          &lt;/dxmvvm:Interaction.Behaviors&gt;
  &lt;/Canvas &gt;
  </code></pre>
  
  <p>i am trying to pass the canvas into my view model.</p>
  
  <p>this is the code for my ViewModel</p>
  
  <pre><code>public DelegateCommand&lt;Canvas&gt; OnSelectedCommand { get; private set; }
          public Vm()
          {
              OnSelectedCommand = new DelegateCommand&lt;Canvas&gt;(OnSelectedEvent, true);
          }
          Canvas c = new Canvas();
          private void OnSelectedEvent(object e)
          {
              if (e == null) return;
              c = e as Canvas;
  
              // Remove selection on clicking anywhere the window
              if (selected)
              {
                  selected = false;
                  if (selectedElement != null)
                  {
                      // Remove the adorner from the selected element
                      aLayer.Remove(aLayer.GetAdorners(selectedElement)[0]);
                      selectedElement = null;
                  }
              }
  
              // If any element except canvas is clicked, 
              // assign the selected element and add the adorner
              if (e.Source != myCanvas)
              {
                  _isDown = true;
                  _startPoint = e.GetPosition(myCanvas);
  
                  selectedElement = e.Source as UIElement;
  
                  _originalLeft = Canvas.GetLeft(selectedElement);
                  _originalTop = Canvas.GetTop(selectedElement);
  
                  aLayer = AdornerLayer.GetAdornerLayer(selectedElement);
                  aLayer.Add(new ResizingAdorner(selectedElement));
                  selected = true;
                  e.Handled = true;
              }
          }
  </code></pre>
  
  <p>currently my code doesn't work. In the code behind i have the <code>MouseButtonEventArgs</code> which is needed in the method. </p>
  
  <p><strong>the problem i am facing now is how do it access the <code>MouseButtonEventArgs</code>?</strong></p>
  
  <p>Without it. I can't use the sample code from the blog</p>