1 year ago

#380912

test-img

knp

Prism 8 WPF DelegateCommand ObservesProperty does not observe the property

I tried to use a DelegateCommand to obsereve a property in a model class (that implements INotifyPropertyChanged). But this works only if the observed Property has a boolean return type.

Is something wrong with my code? Or is this a bug?

public MainWindowViewModel(IMyModel myModel)
{
   MyModel = myModel;
   ExecuteCommand = new DelegateCommand(Execute, CanExecuteModel).ObservesProperty(() => MyModel.HasType); // Works
   ExecuteCommand = new DelegateCommand(Execute, CanExecuteModel).ObservesProperty(() => MyModel.ObjectType); // Does not work
}

private bool CanExecuteModel()
{
   return MyModel.ObjectType != null;
}

The Model:

    public class MyModel : IMyModel, INotifyPropertyChanged
    {
        private Type _objectType;

        public bool HasType
        {
            get => _objectType != null;
        }

        public Type ObjectType
        {
            get => _objectType;
            private set
            {
                _objectType = value; 
                OnPropertyChanged();
                OnPropertyChanged(nameof(HasType));
            }
        }

        public void SetType(Type type)
        {
            ObjectType = type;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

c#

.net

wpf

prism

0 Answers

Your Answer

Accepted video resources