個人的には、Expression BlendSDKInvokeCommandActionの使用を検討します。
うまくいった例をノックしました。ビューは次のとおりです。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" x:Class="WpfApplication1.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<TextBox Text="Hello World">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding DoItCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<Button x:Name="CommandButton" Command="{Binding DoItCommand}" Content="Click me" />
</StackPanel>
</Grid>
</Window>
そして、非常に単純なViewModel(PRISMのDelegateCommandを使用):
public class SomeViewModel
{
public SomeViewModel()
{
DoItCommand = new DelegateCommand(() => Debug.WriteLine("It Worked"));
}
public ICommand DoItCommand { get; private set; }
}
単純な背後にあるコードは、ViewModelを配線します。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new SomeViewModel();
}
}
これを行うためにExpressionBlendを使用する必要もありません。SDKは無料でダウンロードして使用できます。
Expression Blend SDKを使いたくない場合は、MVVMLightが同様のEventToCommandを提供します。
もちろん、これを行うことは、ボタンを配置する理由がまだある場合(または、コマンドの実行可能なロジックを利用したい場合)にのみ実際に意味があります。そうでない場合は、プロパティのセッターでロジックを実行できます。