XCommandオープン ソース コードプレックス wpf 拡張プロジェクト xcommand.codeplex.comで見つけることができます。これにより、MouseMove、MouseLeftButtonDown などの任意のイベントの Command および CommandParameter を、WPF UIElement から継承する任意の UI 要素にバインドできます。
クラス ライブラリの Windows ストア 8 アプリケーションおよび Windows デスクトップ アプリケーション バージョンは、こちらにあります。WPF デスクトップ アプリケーションを扱う WPFXCommand が必要です。ここで、それがどのように機能するか。希望するプロジェクトへの参照としてWPFXCommand.dllを追加します。
以下のように、xaml ファイルに名前空間を追加します。
xmlns:XCmd="clr-namespace:WPFXCommand;assembly=WPFXCommand"
これで、 CommandとCommandParameterを、以下のように WPF UIElement から継承する任意の UI 要素で使用可能なイベントにバインドできます。
<Grid>
<TextBlock Margin="20,30,20,0" VerticalAlignment="Top" Height="80" x:Name="XTextBlock"
Foreground="{Binding FgColor, Mode=TwoWay}"
XCmd:MouseMove.Command="{Binding TextBlockPointerMovedCommand}"
XCmd:MouseLeftButtonDown.Command="{Binding TextBlockPointerPressedCommand}"
XCmd:MouseLeave.Command="{Binding TextBlockPointerExitedCommand}"
Text="{Binding Description, Mode=TwoWay}">
</TextBlock>
<Grid Grid.Column="1" Background="{Binding BgColor, Mode=TwoWay}"
XCmd:MouseMove.Command="{Binding GridPointerMovedCommand}"
XCmd:MouseMove.CommandParameter="{Binding ElementName=XTextBlock, Path=Text}"
XCmd:MouseLeftButtonDown.Command="{Binding GridPointerPressedCommand}"
XCmd:MouseLeftButtonDown.CommandParameter="{Binding ElementName=XTextBlock, Path=Text}"
>
</Grid>
</Grid>
これが、イベント ベースのコード ビハインドを取り除くのに役立つことを願っています。