执行命令不会在后面的资源字典代码中触发
我为它创建了资源字典和代码隐藏文件。 在XAML中,我定义了命令绑定并添加了Executed handler:
这是代码背后:
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; } }
这不是将命令绑定到按钮的方式。 它应该看起来像这样:
... ...
在代码隐藏中:
private void Search_Executed(object sender, ExecutedRoutedEventArgs e) { // do something } private void Search_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = ...; // set to true or false }