2
votes

I am trying to use the latest Preview version of MVVM-Light (V4 - BL16 - Mix11) in my WPF v4 app that was using MVVM-light V3 Sp1.

In my ViewModel, I have the following property defined

    private WifiNetwork _selectedNetwork;
    public WifiNetwork SelectedNetwork 
    {
        get
        {
            return _selectedNetwork;
        }
        set
        {
            if (_selectedNetwork == value)
                return;

            _selectedNetwork = value;
            RaisePropertyChanged("SelectedNetwork");
        }
    }

This property is binded to ListBox's SelectedItem in the View XAML.

I have the following RelayCommand defined in my viewModel

    private RelayCommand _connectCommand;
    public RelayCommand ConnectCommand
    {
        get
        {
            if (_connectCommand == null)
            {
                _connectCommand = new RelayCommand(
                    () => ConnectToSpecifiedNetwork(SelectedNetwork),
                    () => SelectedNetwork != null && ! SelectedNetwork.IsConnected);
            }

            return _connectCommand;
        }
    }

This RelayCommand is binded to my "Connect" button.

When the selected item is not already connected (IsConnected is false), then the connect button should enable.

This worked perfect in MVVM-Light V3 Sp1. But when I use the MIX 11 preview version of MVVM-Light, this doesn't seem to work anymore.

Any suggestions?

I can upload my entire project somewhere if needed..

2
In order to fix this issue, I had to call "RaiseCanExecuteChanged()" method on my INPC property. I didn't have to do this (manually raise the RaiseCanExecuteChanged on a command) in MVVM-Light V3 Sp1.Bradman

2 Answers

1
votes

The reason is that the call to the CommandManager was removed. If you add this back, you won't have to manually raise it. This won't work in silverlight though. Here is the code from V3 that appears to be removed:

    /// <summary>
    /// Occurs when changes occur that affect whether the command should execute.
    /// </summary>
    public event EventHandler CanExecuteChanged
    {
        add
        {
            if (_canExecute != null)
            {
                CommandManager.RequerySuggested += value;
            }
        }

        remove
        {
            if (_canExecute != null)
            {
                CommandManager.RequerySuggested -= value;
            }
        }
    }

    public void RaiseCanExecuteChanged()
    {
#if SILVERLIGHT
        var handler = CanExecuteChanged;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
#else
        CommandManager.InvalidateRequerySuggested();
#endif
    }

Link to V3 RelayCommand

Link to V4 RelayCommand

1
votes

In order to fix this issue, I had to call "RaiseCanExecuteChanged()" method on my INPC property as shown below

    private WifiNetwork _selectedNetwork;
    public WifiNetwork SelectedNetwork 
    {
        get
        {
            return _selectedNetwork;
        }
        set
        {
            if (_selectedNetwork == value)
                return;

            _selectedNetwork = value;

            RaisePropertyChanged("SelectedNetwork");
            // FIX - raise the CanExecuteChanged on the command.
           _connectCommand.RaiseCanExecuteChanged();
        }
    }

I didn't have to do this (manually raise the RaiseCanExecuteChanged on a command) in MVVM-Light V3 Sp1.