Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Execute命令未在资源字典代码中激发_C#_Wpf_Commandbinding - Fatal编程技术网

C# Execute命令未在资源字典代码中激发

C# Execute命令未在资源字典代码中激发,c#,wpf,commandbinding,C#,Wpf,Commandbinding,我已经为它创建了资源字典和代码隐藏文件。 在XAML中,我定义了命令绑定并添加了执行处理程序: <Button Grid.Row="2" Width="100" > <CommandBinding Command="Search" Executed="CommandBinding_Executed" /> </Button> 我不知道为什么在单击按钮时命令不执行,还有,为什么在我没有将CanExecute设置为true时按钮被启用。我也尝试将其设置为真,

我已经为它创建了资源字典和代码隐藏文件。 在XAML中,我定义了命令绑定并添加了执行处理程序:

<Button Grid.Row="2" Width="100" >
  <CommandBinding Command="Search" Executed="CommandBinding_Executed" />
</Button>
我不知道为什么在单击按钮时命令不执行,还有,为什么在我没有将CanExecute设置为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;
        }
    }

这不是将命令绑定到按钮的方式。它应该是这样的:

<Grid>
  <Grid.CommandBindings>
    <CommandBinding Command="Search" 
                    Executed="Search_Executed"
                    CanExecute="Search_CanExecute" />
  </Grid.CommandBindings>
  ...
  <Button Grid.Row="2" Width="100" Command="Search" />
  ...
</Grid>
<Grid>
  <Grid.CommandBindings>
    <CommandBinding Command="Search" 
                    Executed="Search_Executed"
                    CanExecute="Search_CanExecute" />
  </Grid.CommandBindings>
  ...
  <Button Grid.Row="2" Width="100" Command="Search" />
  ...
</Grid>
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
}