0

Ok, so I have property public Person ActualPerson { get; set; } in my code-behind. I set DataContext like this: this.DataContext = this; .

In XAML I have bind DataContext in StackPanel like this: DataContext="{Binding ActualPerson,UpdateSourceTrigger=PropertyChanged}". And in each TextBlock: Text="{Binding Path=Name,UpdateSourceTrigger=PropertyChanged}"

My problem is, when I start my app I have properties form my object and all TextBlocks are filled with data, but when Person class object change, then view didn't refresh values. My Person class implement INotifyPropertyChanged. What I'm doing wrong? Should my UserControll class implement INotifyPropertyChanged rather then Person class?

ext
  • 128
  • 1
  • 7

1 Answers1

0

Your problem is that you're not actually calling PropertyChanged for the ActualPerson object, so the binding on the DataContext won't be updated.

public Person ActualPerson
{
    get { return this.actualPerson; }
    set
    {
        if (this.actualPerson == value)
            return;

        this.actualPerson = value;
        this.OnPropertyChanged("ActualPerson");
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName)
{
    if (this.PropertyChanged != null)
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

EDIT:

It would help to see your ViewModel and code-behind to see how you're handling this. If, for example, you're setting the ActualPerson object that's defined on your UserControl, you may be better off using a DependencyProperty.

public static readonly DependencyProperty ActualPersonProperty = DependencyProperty.Register(
    "ActualPerson", typeof (Person), typeof (MyUserControl), new PropertyMetadata(default(Person)));

public Person ActualPerson
{
    get { return (Person) GetValue(ActualPersonProperty); }
    set { SetValue(ActualPersonProperty, value); }
}
Matt
  • 2,682
  • 1
  • 17
  • 24
  • Also remember to specify the INotifyPropertyChanged interface. – Holstebroe Jan 11 '14 at 17:05
  • Ok, but my Person class implements INotifyPropertyChanged. So my code-behind class should implement that interface? – ext Jan 11 '14 at 17:10
  • Well your binding needs to be notified somehow of a change. Instead of using `INotifyPropertyChanged` in your code-behind, I would go with the suggestion I added in with my edit of using a `DependencyProperty` @ext – Matt Jan 11 '14 at 17:12
  • Option with `DependencyProperty` works, but how? And I want to know, `INotifyPropertyChanged` should be implemented in model class like mine (Person class)? Or Window / UserControl class? – ext Jan 11 '14 at 17:18
  • INotifyPropertyChanged should be implemented on any class that's used in bindings, either WPF bindings or in controls like a grid where you want it to know of property changes for each row. In order to be able to use a DependencyProperty, you need to use a class that derives from `DependencyObject`, which rules out most classes aside from the code-behind. You may want to take a look at [this SO question](http://stackoverflow.com/questions/291518/inotifypropertychanged-vs-dependencyproperty-in-viewmodel) – Matt Jan 11 '14 at 17:27