How to automatically enable/disable the command attached control with 'CanExecute'

Jun 22, 2011 at 4:12 AM
Edited Jun 22, 2011 at 6:02 AM

How to combine enabling/disabling the command attached control which have CanExecute, on Simple MVVM Toolkit for Windows Phone 7.0?

I have tested following example, but button not to be disabled when CanExecute return false. Currently I bind IsEnabled to CheckCondition method...

MVVM Light Toolkit have option attribute named 'MustToggleIsEnabled' to solve this case.

Any suggestion?

 

for example.

in xaml.

<Button Content="DoSomething">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <SimpleMvvmToolkit:EventToCommand Command="{Binding DoSomethingCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

in C#.

public ICommand ButtonClickCommand
{
    get
    {
        return new DelegateCommand
        (
            DoSomething, CanDoSomething
        );
    }
}

void DoSomething()
{
    MessageBox.Show("OK!");
}

bool CanDoSomething()
{
    // return true or false by condition.
    return CheckConditionToDoSomething();
}

 
Jun 22, 2011 at 7:27 AM
Edited Jun 22, 2011 at 7:28 AM

OK. The 'MustToggleIsEnabled' is ready for Simple MVVM Toolkit.

But it causes xaml compile error.

in xaml.

<Button Content="DoSomething">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <SimpleMvvmToolkit:EventToCommand
                Command="{Binding DoSomethingCommand}"
                MustToggleIsEnabled="True"
                />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

error.

System.Windows.Application.LoadComponent(
    this,
    new System.Uri(
        "/SimpleMvvmTestApp;component/MainPage.xaml",
        System.UriKind.Relative
));
Jun 22, 2011 at 7:43 AM

Sorry, I was confused.

I understand that 'MustToggleIsEnabledValue' should be used for boolean value True/False, not 'MustToggleIsEnabled'.

Now it works fine for me. :)

<Button Content="DoSomething">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <SimpleMvvmToolkit:EventToCommand
                Command="{Binding DoSomethingCommand}"
                MustToggleIsEnabledValue="True"
                />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>
Coordinator
Jun 29, 2011 at 6:11 PM

Glad you figured it out. :-)

Tony