Is it possible to instruct Fody to generate PropertyChanged Events for the Properties Color1 and Color2 if the corresponding Items in the ObservableCollection _colors are changing?
public class MyModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Name { get; set; }
public Brush Color1 => _colors[0];
public Brush Color2 => _colors[1];
private ObservableCollection<Brush> _colors;
public MyModel()
{
_colors = new ObservableCollection<Brush>()
{
Brushes.Transparent,
Brushes.Black,
};
}
public void DoSomething()
{
_colors[0] = Brushes.Green;
_colors[1] = Brushes.Red;
}
protected void OnPropertyChanged(PropertyChangedEventArgs eventArgs)
{
PropertyChanged?.Invoke(this, eventArgs);
}
}
For the Name Property the Event will be generated, but not for Color1 and Color2 (see extract from ILSpy):
public string Name
{
[CompilerGenerated]
get
{
return <Name>k__BackingField;
}
[CompilerGenerated]
set
{
if (!string.Equals(<Name>k__BackingField, value, StringComparison.Ordinal))
{
<Name>k__BackingField = value;
OnPropertyChanged(<>PropertyChangedEventArgs.Name);
}
}
}
public Brush Color1 => _colors[0];
public Brush Color2 => _colors[1];
Currently Color1
and Color2
are read-only properties, so there is no need generate PropertyChanged
events for them. What you want is code to fire the event for property A
when field b
is set. I doubt that any code generator will do that (fields don't have setters, where the event needs to be invoked).
The Fody
docs state that
All classes that implement INotifyPropertyChanged will have notification code injected into property setters.
So, maybe your best try is to create properties with private setters (and remove your fields _color1&2
).