1

I have successfully bound a control to a property on my page. The property I am binding to returns a class. The binding works but if I set the property to be a new instance of the class the UI is not updated.

Could anyone point be in the right direction to solve this issue, I have tried implementing INotifyPropertyChanged but this doesn't work.

The Code is as follows..

XAML - Very Basic just trying to bind a label to a property at the minute.

<Grid> <Label Content="{Binding StudentName}"></Label> <Button Width="100" Height="75" Click="Button_Click" ></Button> </Grid> 

C#

this.DataContext = parentWindow.SelectedStudent; 

The parent Window C# note parent window implements INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged; public UCStudent SelectedStudent { get { if (_selectedStudent == null) { _selectedStudent = new UCStudent(); } return _selectedStudent; } set { if (_selectedStudent != value) { _selectedStudent = value; OnPropertyChanged("SelectedStudent"); } } } protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } 

The issue is when I set the SelectedStudent property to be a new selected student the binding does not update.

6
  • 1
    post the code of your ViewModel and the corresponding XAML ... Commented Jul 2, 2012 at 22:08
  • You should probably post your implementation of INotifyPropertyChanged, because you need that in order for this to work. When your setter changes, you need to fire the PropertyChanged event. Are you doing that? Commented Jul 2, 2012 at 22:37
  • There is no connection between the code and XAML you have posted. What is Test? INotifyPropertyChanged is required to update the UI if the property changes. Commented Jul 2, 2012 at 22:39
  • Hi Bob ill modify it now, i am raising the on property change event but the handler is always null and so is not firing if I change the property to type string and change the value the PropertyChange event fires and the UI updates. Commented Jul 2, 2012 at 22:39
  • Sorry evan i have been messing with the code while debugging this ill update it Commented Jul 2, 2012 at 22:40

1 Answer 1

1

While my approach uses generics and an expression, so I can strongly type my property changes, the concept is the same. Where I use an expression, you use a string. You'll want to return if PropertyChanged is null. Otherwise, fire the event without declaring a new event handler first.

public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged<T>(Expression<Func<T>> expression) { if (this.PropertyChanged == null) { return; } string propertyName = GetPropertyName(expression); PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } 

This approach allows the view model to strongly type the notification.

this.NotifyPropertyChanged(() => this.SelectedApplication); 
Sign up to request clarification or add additional context in comments.

3 Comments

If it would help to change my method to show the string version, let me know. Hope it helps.
Don't forget the most important part, i.e. that the class needs to implement the INotifyPropertyChanged interface for the whole thing to work.
Right; the OP already has that part. But maybe I should update my answer to show the full thing...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.