jauchand
jauchand

Reputation: 27

Fody.PropertyChanged: RaisePropertyChanged does not work for DependsOn Properties

For building the WPF application I used VS2022 (V17.4.4) and NuGet Fody.PropertyChanged Package (V4.1.0).

If a property is set directly, the PropertyChanged event will be raised for the property itself and all the [DependsOn] properties as well. But if only the underlying field of the property is set and the PropertyChanged event is raised later manually by call of RaisePropertyChanged(nameof(Property)) the [DependsOn] properties will not raise any PropertyChanged event.

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace FodyPropertyChanged
{
  [PropertyChanged.AddINotifyPropertyChangedInterface]
  public class MainWindowViewModel : INotifyPropertyChanged
  {
    private DeviceStates _DeviceState;
    private event PropertyChangedEventHandler _propertyChanged;
    
    
    public event PropertyChangedEventHandler PropertyChanged
    {
      add { _propertyChanged += value; }
      remove
      {
        if (_propertyChanged != null)
          _propertyChanged -= value;
      }
    }

    public DeviceStates DeviceState
    {
      get { return _DeviceState; }
      set { _DeviceState = value; }
    }

    [PropertyChanged.DependsOn(nameof(DeviceState))]
    public bool IsBusy
    {
      get { return DeviceState == DeviceStates.Working; }
    }

    public void SetDeviceStateTo(DeviceStates deviceState)
    {
      DeviceState = deviceState;
    }

    public void SetDeviceStateToAndRaisePropertyChangedAfterwards(DeviceStates deviceState)
    {
      _DeviceState = deviceState;

      RaisePropertyChanged(nameof(DeviceState));
    }

    private void RaisePropertyChanged([CallerMemberName] string propertyName = "")
    {
      PropertyChangedEventHandler handler = _propertyChanged;
      if (handler != null)
      {
        handler(this, new PropertyChangedEventArgs(propertyName));
      }
    }
  }
}

If I call SetDeviceStateTo(...) the PropertyChanged event will be raised for DeviceState and IsBusy (GUI will be updated, correctly). But if I call SetDeviceStateToAndRaisePropertyChangedAfterwards(...) the PropertyChanged event will be raised for DeviceState, only (IsBusy bindings will not be updated).

Any idea?

Upvotes: 1

Views: 552

Answers (1)

jauchand
jauchand

Reputation: 27

Like canton7 and BionicCode said: [DependsOn] is only working for the setter not for events. So it is a known limitation and not a bug.

Upvotes: 0

Related Questions