I'm working on a WPF project. To prevent a lot of boiler code I'm using Fody PropertyChanged. This is a snippet of my problem:
[ImplementPropertyChanged]
public class MyViewModel : MyViewModelBase
{
public bool HasCardSet
{
get
{
return Person != null && Person.Card != null;
}
}
public void SavePerson()
{
Person.Card = new Card();
}
}
[ImplementPropertyChanged]
public class MyViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Person Person { get; set; }
}
[ImplementPropertyChanged]
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Card Card { get; set; }
}
[ImplementPropertyChanged]
public class Card : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Name { get; set; }
public ObservableCollection<SomeOtherClass> Items { get; set; }
}
When I call SavePerson()
(in MyViewModel
), It won't trigger the HasCardSet
property.
I've added the PropertyChangedNotificationInterceptor
and see that property "Card" changes on target "People". But my breakpoint in HasCardSet
does not trigger.
Your HasCardSet
property has to be declared in the Person
class.
Then, as suggested in the documentation, add this method :
public void OnCardChanged()
{
HasCardSet = Card != null;
}
This will trigger the expected PropertyChanged
event.
Note that your Binding
should have your Person
object as DataContext
, not directly your View Model.