2011-10-21 4 views
1

ICommand를 컨트롤의 이벤트에 연결하는 다양한 구현 방법을 살펴보고 있습니다. 예를 들어, TextBox의 GotFocus는 내 View Model에서 GotFocusCommand를 호출해야합니다. 그런 다음 내 자신의 학습용으로 자체 버전을 구현할 생각이 들었지만 잘 작동하지만 XAML에서는 하나의 이벤트 만 하나의 명령에 연결할 수 있습니다.WPF 이벤트에 대한 또 다른 구현 (문제 있음)

<Button 
    local:EventToCommand.Event="Click" 
    local:EventToCommand.Command="{Binding TestCommand}" 
    /> 

을하지만 그렇지 않은 :

<Button 
    local:EventToCommand.Event="Click" 
    local:EventToCommand.Command="{Binding ClickCommand}" 
    local:EventToCommand.Event="GotFocus" 
    local:EventToCommand.Command="{Binding GotFocusCommand}" 
    /> 
(기본적으로 난 그냥 지정된 이벤트를 찾기 위해 반사를 사용하여 다음 명령을 실행하는 AddEventHandler 할)이 잘 작동

중복되면 속성 이름 오류가 발생합니다.

<Button> 
    <Some Xaml Element> 
    <local:EventToCommand Event="Click" Command="{Binding ClickCommand}" /> 
    <local:EventToCommand Event="GotFocus" Command="{Binding GotFocusCommand}" /> 
    </Some Xaml Element> 
</Button> 

명령을 여러 이벤트를 "맵":

는 같은 일을 할 수 있을까?

답변

1

첨부 된 속성을 사용하거나 Button에서 상속하거나 EventToCommand 개체 목록이 포함 된 자체 DependencyProperty를 추가하는 두 가지 방법이 있습니다. 컬렉션에 추가하면 이벤트를 연결합니다. 명령. 이 부분이 혼란 스럽다면 몇 가지 예를 들어 보겠습니다.

C#을

public class EventedButton : Button 
{ 
    public static DependencyProperty EventCommandsProperty 
     = DependencyProperty.Register("EventCommands", typeof(EventToCommandCollection), typeof(EventedButton), new PropertyMetadata(null)); 


    public EventToCommandCollection EventCommands 
    { 
     get 
     { 
      return this.GetValue(EventCommandsProperty) as EventToCommandCollection; 
     } 
     set 
     { 
      this.SetValue(EventCommandsProperty, value); 
     } 
    } 

    public EventedButton() 
    { 
     this.EventCommands = new EventToCommandCollection(this); 
    } 
} 

XAML :

<local:EventedButton> 
     <local:EventedButton.EventCommands> 
      <local:EventToCommand /> 
     </local:EventedButton.EventCommands> 
    </local:EventedButton> 

EventToCommandCollection의 내부, 당신은/첨부 항목이 컬렉션에 추가 할 때 당신이 원하는 이벤트를 분리합니다.

업데이트 : 혼합 이벤트를 사용

<local:EventedButton> 
     <local:EventToCommand.Commands> 
      <local:EventToCommandCollection> 
       <local:EventToCommand/> 
      </local:EventToCommandCollection> 
     </local:EventToCommand.Commands> 
    </local:EventedButton> 
+0

을 네임 스페이스이

<Button> <i:Interaction.Triggers> <i:EventTrigger EventName="Click" > <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding ButtonClick}" /> </i:EventTrigger> </i:Interaction.Triggers> <i:Interaction.Triggers> <i:EventTrigger EventName="GotFocus" > <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding ButtonGotFocus}" /> </i:EventTrigger> </i:Interaction.Triggers> </Button> 

어디에서 가져 오기를 수행 할 수 있습니다 현재 구현은 ba입니다. 이벤트 및 커맨드가 모두 AP이기 때문에 첨부 된 속성에 sed. 그 구현을 사용하여 XAML에서 여러 EventToCommand를 지정하는 방법을 잘 모르겠습니다. 상속 옵션) – Oliver

+0

상속 옵션은 괜찮지 만 XAML에서 EventToCommand 컬렉션을 초기화하는 방법은 무엇입니까? – Oliver

+0

일부 코드로 내 대답을 업데이트했습니다. – mekansm

1

:

C#

 public static DependencyProperty CommandsProperty = 
     DependencyProperty.RegisterAttached(
     "Commands", 
     typeof(ICollection<EventToCommand>), 
     typeof(DependencyObject), 
     new PropertyMetadata(null, OnCommandsChanged)); 


    private static void OnCommandsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     // Attach/Detach event handlers 
    } 

    public static void SetCommands(DependencyObject element, ICollection<EventToCommand> value) 
    { 
     element.SetValue(CommandsProperty, value); 
    } 

    public static ICollection<EventToCommand> GetCommands(DependencyObject element) 
    { 
     return (ICollection<EventToCommand>)element.GetValue(CommandsProperty); 
    } 

XAML : 여기

재산권

첨부가 연결된 속성으로 컬렉션을 수행하는 몇 가지 코드 트리거 및 조치는 사용자 자신의 콜렉션을 처리 할 필요성을 무효화합니다. 그리고 모든 컨트롤에 추가 할 수 있습니다.

참조 MVVM 조명 EventToCommand

아니면 내 확장 here.(source)

+0

블렌드가 없다는 것을 잊어 버렸으므로 Microsoft.Expression.Interactivity dll을 가지고 있지 않습니다 .... – Oliver

0

GalaSoft MVVM 라이트 툴킷은 - EventToCommand이이

i- xmlns:i="clr-namespace:System.Windows.Interactivity; 
    assembly=System.Windows.Interactivity" 
cmd-xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command; 
    assembly=GalaSoft.MvvmLight.Extras.WPF4"