2012-11-20 49 views
1

我正在學習MVVM設計模式,所以我試圖將某些操作更改爲Command。
下面是一個示例,MainWindow具有一個Canvas作爲容器,用戶可以通過拖動繪製矩形。所以我寫的代碼如下
如何將三步操作轉換爲WPF中的命令?

protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) 
{ 
    base.OnMouseLeftButtonDown(e); 
    StartPoint = e.GetPosition(this);  
    shape = new Rectangle(); 
    shape.Fill = Brushes.Transparent; 
    shape.Stroke = Brushes.Black; 
    shape.StrokeThickness = 1; 
    this.Children.Add(shape); 
} 

protected override void OnMouseMove(MouseButtonEventArgs e) 
{ 
    Point endpoint = e.GetPosition(this); 
    double left = Math.Min(endpoint.X, StartPoint.X); 
    double top = Math.Min(endpoint.Y, StartPoint.Y); 
    shape.Margin = new Thickness(left, top, 0, 0); 
    shape.Width = Math.Abs(endpoint.X - StartPoint.X); 
    shape.Height = Math.Abs(endpoint.Y - StartPoint.Y); 
    shape.Stroke = Brushes.Black; 
    shape.StrokeThickness = 2; 
} 

protected override void OnMouseLeave(MouseButtonEventArgs e) 
{ 
    //end 
} 

因爲也許我想補充Undo功能,使矩形將會消失撤消調用後,所以我希望讓這些3個步驟爲一個命令。我應該怎麼做?謝謝。

+0

我想你需要的所有3種方法,雖然可以添加/刪除在'MouseDown'和'MouseUp'事件的相關處理程序所以你只需要將一個事件連接到你的UI。至於撤銷功能,當在'MouseUp'的'List'中創建每個矩形時,如何存儲每個矩形,以便在撤消 – Rachel

+0

時刪除列表中的最後一項。 – Alan

回答

1

微軟的Expression Blend Behaviors可以做到這一點。要實現和使用自己的行爲,您不需要Expression Blend,只需要可供下載的SDK。

它的工作方式是實現行爲,其中T:DependencyObject。這個類有兩個可重寫的方法OnAttach()和OnDetach(),它們可以與你的事件連接或斷開,並將上述邏輯放在行爲中。如果你命名你的類DrawRectangleBehavior,然後,所有你需要做的是:

.... 
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
.... 

<Canvas> 
    <i:Interaction.Behaviors> 
     <myBlend:DrawRectangleBehavior /> 
    </i:Interaction.Behaviors> 
</Canvas> 

和行爲(我沒有測試這個)

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 
using System.Windows.Interactivity; 
using System.Windows.Media; 
using System.Windows.Shapes; 

namespace MyCompany.Common.Behaviors 
{ 
    public class DrawRectangleBehavior : Behavior<Canvas> 
    { 
     Point StartPoint; 
     Shape shape; 

     protected override void OnAttached() 
     { 
      AssociatedObject.PreviewMouseLeftButtonDown += OnMouseLeftButtonDown; 
      AssociatedObject.PreviewMouseMove += OnMouseMove; 
      AssociatedObject.MouseLeave += OnMouseLeave; 
     } 

     protected override void OnDetaching() 
     { 
      AssociatedObject.PreviewMouseLeftButtonDown -= OnMouseLeftButtonDown; 
     AssociatedObject.PreviewMouseMove -= OnMouseMove; 
     AssociatedObject.MouseLeave -= OnMouseLeave; 
     } 


     protected void OnMouseLeftButtonDown(object o, MouseButtonEventArgs e) 
     { 
      StartPoint = e.GetPosition(AssociatedObject); 
      shape = new Rectangle(); 
      shape.Fill = Brushes.Transparent; 
      shape.Stroke = Brushes.Black; 
      shape.StrokeThickness = 1; 
      AssociatedObject.Children.Add(shape); 
     } 

     protected void OnMouseMove(object o, MouseEventArgs e) 
     { 
      Point endpoint = e.GetPosition(AssociatedObject); 
      double left = Math.Min(endpoint.X, StartPoint.X); 
      double top = Math.Min(endpoint.Y, StartPoint.Y); 
      shape.Margin = new Thickness(left, top, 0, 0); 
      shape.Width = Math.Abs(endpoint.X - StartPoint.X); 
      shape.Height = Math.Abs(endpoint.Y - StartPoint.Y); 
      shape.Stroke = Brushes.Black; 
      shape.StrokeThickness = 2; 
     } 

     protected void OnMouseLeave(object o, MouseEventArgs e) 
     { 
      //end 
     } 

    } 
} 

和你有一個可重複使用的一塊碼。

請參閱下面的教程WPF Tutorial | Blend Behaviors

而下面的下載鏈接Expression Blend SDK

相關問題