2012-11-20 8 views
1

일부 작업을 명령으로 변경하려고하므로 MVVM 디자인 패턴을 배우고 있습니다.
예를 들어, MainWindow는 컨테이너로 Canvas를 가지며 사용자는 드래그를 통해 사각형을 그릴 수 있습니다. 그래서 나는 사각형이 취소 호출 후 사라집니다 있도록 실행 취소 기능을 추가 싶어 어쩌면 이후 3 단계 작업을 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 
} 


아래로 코드를 작성, 그래서 나는 한 명령에서 이러한 3 단계를 만들고 싶어. 어떻게해야합니까? 감사.

+0

따라서 UI에 하나의 이벤트 만 연결하면됩니다. Undo 기능에 관해서는 각 사각형을'MouseUp'의'List'에서 생성 할 때마다 저장하려고합니다. 그래서'List'에서 Undo의 마지막 항목을 제거 할 수 있습니다. – Rachel

+0

질문에 대한 답변이 있습니까? – Alan

답변

1

Microsoft의 Expression Blend Behaviors가이 작업을 수행합니다. 자신 만의 비헤이비어를 구현하고 사용하려면 다운로드 할 수있는 SDK 인 Expression Blend가 필요하지 않습니다.

작동 원리는 Behavior where T : DependencyObject를 구현하는 것입니다. 이 클래스에는 이벤트에 연결하고 연결을 해제하는 OnAttach() 및 OnDetach()라는 두 가지 대체 가능한 메서드가 있으며 위의 논리를 동작 내부에 넣습니다.

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 
     } 

    } 
} 

(나는 이것을 테스트하지 않았다)

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

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

그리고 동작을 그리고 당신의 재사용 가능한 조각이 : 당신이 당신의 클래스 DrawRectangleBehavior 이름을한다면, 다음, 당신이해야 할 모든이입니다 암호.

다음 튜토리얼 WPF Tutorial | Blend Behaviors

그리고 다음 다운로드 링크를 참조하십시오 난 당신이 추가 /`MouseDown`와`MouseUp` 이벤트에 관련 핸들러를 제거 할 수 있지만 당신이 3 가지 방법이 필요하다고 생각 Expression Blend SDK