4

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.

RvdK
  • 19,580
  • 4
  • 64
  • 107
  • how would binding now, that HasCardSet has changed, if it's not a part of a Person? – netaholic Sep 22 '15 at 11:07
  • Well, my Person has another property Info (class), and that has a string property Name. (Person.Info.Name) If I change it in code, my binding in XAML gets updated. So why does that work, and this doesn't? – RvdK Sep 22 '15 at 11:26
  • @OP: It depends a lot on the way you update the property (do you set the Person's Info property or just the Name of this property ?) and the binding itself. More of your code may help... – xum59 Sep 22 '15 at 11:44

1 Answers1

6

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
xum59
  • 841
  • 9
  • 16
  • Moved HasCardSet to Person, and updated XAML to listen to Person.HasCardSet. This worked! Didn't use the OnCardChanged. – RvdK Sep 22 '15 at 12:42