views:

582

answers:

2

Hi everybody,

I try to do a custom CanExecuteChanged event for a command button. Inside CanExecuteChanged event I would like to do some stuff when canExecute value change but I don't want to do it by implementing a custom command button class (deriving from Button and Implementing ICommandSource). Also I don't want to do my stuff into CanExecute method.

Any ideas?

Thanks.

A: 

You can handle the CanExecuteChanged event of the command

Thomas Levesque
OK, I agree but how to handle and implement it? I would like to do the same as Executed and CanExecute methods, for example:private void CanExecuteChanged(object sender, EventArgs e){ // Do my stuff}My problem is that I don't know how to bind this event to the command button.In my xaml I do: <Page.CommandBindings> <CommandBinding Command="MyCommand" Executed="MyExecuted" CanExecute="MyCanExecute"> </CommandBinding> </Page.CommandBindings> <Button Command="MyCommand" Execute="MyExecute" CanExecuted="MyCanExecuted" ...>
What is "MyCommand" ? How is it defined ? And please, edit your answer to post the code : in the comments, it's unreadable
Thomas Levesque
Sorry, I have posted an example about what I want to do. I have posted it as an answer to make it more readable.
In the Page constructor, just subscribe your `CanExecuteChanged` method to the `MyNameSpace.MyClass.MyRCmd.CanExecuteChanged` event. BTW, you should edit your original question rather than posting an answer.
Thomas Levesque
Ok, thanks! I do what you say and now I can handle when CanExecute changes its value by executing my custom handler for CanExecuteChanged ;)
A: 

For example:

in XAML:

<Page  xmlns:local="clr-namespace:MySolution" ....>
 <Page.CommandBindings>
    <CommandBinding Command="{x:Static local:MyNameSpace.MyClass.MyRCmd}"
               Executed="MyCmdBinding_Executed"
               CanExecute="MyCmdBinding_CanExecute"/>
 </Page.CommandBindings>

  ...

 <Button Command="{x:Static local:MyNameSpace.MyClass.MyRCmd}" ... />

  ...

</Page>

And in page code behind:

namespace MyNameSpace
{

public partial class MyClass : Page
{

    ...

    public static RoutedCommand MyRCmd = new RoutedCommand();

    public event EventHandler CanExecuteChanged;

    private void CanExecuteChanged(object sender, EventArgs e) 
    { 
      // Here is my problem: How to say to execute this when CanExecute value is
      // changing? I would like to execute this on CanExecute value changed.
      // I think somewhere I can tell compiler the handler for CanExecutedChanged is 
      // this. How to?
    } 

    private void MyCmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        // Do my stuff when CanExecute is true
    }

    private void MyCmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        if (....)
        {
            e.CanExecute = true;
        }
        else
        {
            e.CanExecute = false;                
        }
    }

    ...

} // end class

} // end namespace

and my problem is how to say compiler: Hey, on CanExecute value changed you must call and do the stuff into CanExecuteChanged method.

Thanks very much.