views:

169

answers:

1

I have a ListView that is bound to a collection of view models. The Item template contains a button that is bound to a command on the view model. When I set the property that the ItemsSource of the ListView I call RaiseCanExecuteChanged for each viewmodel.

public BindingList<IVehicleViewModel> Vehicles 
    {
        get { return _vehicles; }
        set 
        { 
            if(_vehicles == value) return;
            _vehicles = value;
            OnPropertyChanged("Vehicles");
            RaiseCanExecuteChangedEvents();
        }
    }

Despite the fact that I have verified that true is returned for the CanExecute on all view models the button shows as greyed out. The only thing I have noticed is that if I call RaiseCanExecuteChanged when the listview has already been rendered everything works as expected and if I call it before the listview has been rendered and then scroll through the items they sort themselves out.

A: 

There's a static method CommandManager.InvalidateRequerySuggested which forces a reevaluation of command executability (is that a word?) try calling that whenever you want to make sure your UI updates to reflect a change in the CanExecute result of your command.

Aviad P.
I have created a command that calls CommandManager.InvalidateRequerySuggested(); and it doesn't appear to call CanExecute at all?I am DelegateCommand<object> from the Microsoft.Practices.Composite.Presentation.Commands
Russ
in the RaiseCanExecuteChangedEvents() I have added the line Thread.Sleep(1000); this seems to solve the problem. It seems as I suspected that you have to call the RaiseCanExecuteChangedEvents() after the control has been rendered on the screen. Obviously this is not an acceptable way to solve the problem.
Russ