Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

c# - When is CanExecute called?

In the demo, I have a button to toggle a bool field isAsking. I create a command which can execute only when isAsking==true.

Once I press the Toggle button, okButton.IsEnable changes immediately, which indicates the command finds the change of isAsking.

I feel very confused why the command object notices the change of a field. When CanExecute will be called?

Although writting WPF application for some time, I'm new to WPF Command. Please give an explanation to this case and if possible, point out some related articles or blogs (I've already read too many articles talking about cut/paste command).

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="525" x:Name="mainWindow" >
    <StackPanel>
        <Button Name="okButton" Content="Ok" />
        <Button Content="Toggle"  Click="Button_Click_1"/>
    </StackPanel>
</Window>

Code-behind:

public partial class MainWindow : Window
{
    private bool isAsking;

    public MainWindow()
    {
        InitializeComponent();

        CommandBinding cb = new CommandBinding();
        cb.Command = okCommand;
        cb.CanExecute += CanOKExecute;
        cb.Executed += cb_Executed;
        mainWindow.CommandBindings.Add(cb);
        okButton.Command = okCommand;
    }

    private RoutedCommand okCommand = new RoutedCommand("ok", typeof(MainWindow));


    void cb_Executed(object sender, ExecutedRoutedEventArgs e)
    {
       
    }

    void CanOKExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = isAsking;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        isAsking = !isAsking;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The technical answer is that CanExecute will be invoked whenever the CommandManager.RequerySuggested event is raised. According to the documentation, this will be...

...when the CommandManager detects conditions that might change the ability of a command to execute.

In practical terms, this just means that you don't need to worry about when CanExecute is called: WPF will invoke it when it thinks it is appropriate, and in my experience this will almost always cover your requirements.

The exception to this is if you have a background task that will cause CanExecute to change it's return value based on something that is not triggered by the UI. In this scenario, you may need to manually force the WPF runtime to re-query CanExecute which you can do by calling CommandManager.InvalidateRequerySuggested


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...