リソースディクショナリとそのためのコードビハインドファイルを作成しました。XAMLでは、コマンドバインディングを定義し、Executedハンドラーを追加しました。
<Button Grid.Row="2" Width="100" >
<CommandBinding Command="Search" Executed="CommandBinding_Executed" />
</Button>
コードビハインドは次のとおりです。
partial class StyleResources : ResourceDictionary {
public StyleResources() {
InitializeComponent();
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) {
//this is never executed
}
}
ボタンがクリックされたときにコマンドが実行されない理由と、CanExecuteをtrueに設定しなかったときにボタンが有効になる理由がわかりません。また、trueに設定しようとしましたが、CanExecuteイベントも発生しませんでした。リソースディクショナリの使用方法は次のとおりです。
public partial class MyWindow : Window {
public MyWindow() {
InitializeComponent();
Uri uri = new Uri("/WPFLibs;component/Resources/StyleResources.xaml", UriKind.Relative);
ResourceDictionary Dict = Application.LoadComponent(uri) as ResourceDictionary;
this.Style = Dict["WindowTemplate"] as Style;
}
}