I am using MVVM-Light and i have my relay command working perfectly, I have just read that i should be implementing CanExecuteChanged
and CanExecute
. Although i am unable to find a good example.
Does anyone have a good example of how to implement these.
CanExecute needs to return False when it can't be executed but wouldn't just disbale the button ??
When do i execute the CanExecuteChanged
?
Anyone have any good examples of when to use each one, my code works without but this blog post states that I should be implementing these items.
I am a little confused, as I said I thought I would just bind the Enabled
property or something to a property in the ViewModel so I can disable the button or a similar control?
Any help in understanding would be really grateful.
EDIT
This is what i have now... Its working but the button isn't physically DISABLED only the commmand doesn't run as i am returning false. I am calling CanExecuteMe in the constructor to force the RaiseCanExecuteChanged to run ...
This runs in my construtor of my viewmodel
this.Page2Command = new RelayCommand(() => this.GoToPage2(), () => CanExecuteMe);
CanExecuteMe = false;
and here is the rest of my code, i took it from an example.
private bool _canIncrement = true;
public bool CanExecuteMe
{
get
{
return _canIncrement;
}
set
{
if (_canIncrement == value)
{
return;
}
_canIncrement = value;
// Update bindings, no broadcast
//RaisePropertyChanged(CanIncrementPropertyName);
Page2Command.RaiseCanExecuteChanged();
}
}
public RelayCommand Page2Command
{
get;
private set;
}
private object GoToPage2()
{
System.Windows.MessageBox.Show("Navigate to Page 2!");
return null;
}
And here is my XAML
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="31,77,0,0" x:Name="button1" VerticalAlignment="Top" Width="75" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Page2Command, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>