I just tried to update one of my WPF projects from MVVM Light 4.2.30 to 5.2. After that I noticed that my RelayCommands
do not fire their CanExecute
methods anymore.
After a quick search I found several articles that explain the problem and suggest using the GalaSoft.MvvmLight.CommandWpf
namespace instead of GalaSoft.MvvmLight.Command
. However I cannot find the GalaSoft.MvvmLight.CommandWpf
namespace. When I look at the GalaSoft.MvvMGalaSoft.MvvmLight.dll in Visual Studio's 'Object Browser' then I also cannot find this namespace.
As it seems nobody else but I have that problem - any ideas what I'm doing wrong?
Update:
I've created a small example project that shows how I currently use the RelayCommands with their CanExecute methods in Version 4.2.30 of MVVM light:
public class ViewModel : ViewModelBase
{
private bool _isReadOnly = false;
public ViewModel ()
{
this.DoSomethingCommand = new RelayCommand(DoSomething, CanDoSomething);
}
public bool IsReadOnly
{
get
{
return _isReadOnly;
}
set
{
_isReadOnly = value;
this.RaisePropertyChanged("IsReadOnly");
// With MVVMLight 4.2.30.23246 I did not need to call the RaiseCanExecuteChanged on any of my RelayCommands
// DoSomethingCommand.RaiseCanExecuteChanged();
}
}
public RelayCommand DoSomethingCommand { get; set; }
private bool CanDoSomething()
{
return !this.IsReadOnly;
}
private void DoSomething()
{
MessageBox.Show("Let's break the MVVM idea...");
}
}
The XAML code of the view is:
<Window x:Class="MVVMLight5.2CanExecuteTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MVVMLight5._2CanExecuteTest"
mc:Ignorable="d"
Title="Test" Height="150" Width="200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0" Content="Is read only" IsChecked="{Binding IsReadOnly, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Row="1" Grid.Column="0" Content="Break me" Command="{Binding DoSomethingCommand}"/>
</Grid>
My target is that if I have a button in the View that uses the 'DoSomethingCommand' as a Command then this button should become disabled when my IsReadOnly property is turned to false. When using MVVM light 4.2.30 then this works without any additional so far but in MVVM light 5.2 I need to add the DoSomethingCommand.RaiseCanExecuteChanged(); to make the button go disabled in view.
Can I somehow get the old behavior with the new MVVM light framework?