2

MV-VM とコマンド バインド (RelayCommand) については多くの議論がありますが、MV-VM パターンのハンドラーにバインドするルーティング イベントについてはあまり取り上げられていません。私は最善のアプローチが何であるかを見つけたいと思っています。

カスタム イベントと VM にバインドされたイベント ハンドラーを使用した RoutedEvent バインドの例を次に示します。

<Navigation:LeftNavigation x:Name="_leftNav" Margin="3"
            BindingHelper:EventHelper.RoutedEvent="Events:Customer.SelectionChanged"
            BindingHelper:EventHelper.EventHandler="{Binding SelectionChanged}" />

私の Vm では、これに似たイベント ハンドラーを使用します。

public void SelectionChanged(object sender, CustomerSelectionChangedArgs e)
{
    // Do something
}

これは、コマンド バインディングの多くの例から得られた概念にすぎません。ルーティングされたイベントでこれを機能させるにはどうすればよいですか。

4

1 に答える 1

2

著者が同様の構文を実装しているこの記事を確認できます。

<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" x:Name="test">
  <local:CommandBehaviorCollection.Behaviors>
    <local:BehaviorBinding Event="MouseLeftButtonDown" Action="{Binding DoSomething}" CommandParameter="An Action on MouseLeftButtonDown"/>
    <local:BehaviorBinding Event="MouseRightButtonDown" Command="{Binding SomeCommand}" CommandParameter="A Command on MouseRightButtonDown"/>
  </local:CommandBehaviorCollection.Behaviors>
  <TextBlock Text="MouseDown on this border to execute the command"/>
</Border>
于 2009-10-20T15:32:47.540 に答える