WPF Data Binding to changing object?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

Most UI bindings already handle this via property notifications, in particular (for WPF) INotifyPropertyChanged . - i.e. If you are updating the UserId on a single instance.

Up vote 3 down vote favorite 1 share g+ share fb share tw.

I have a user control which displays the currently logged in user's name. I have bound a TextBlock in the control to the UserId property of a User obejct in my application. The issue I have is that the User object my binding has as a source changes each time a new user logs in.

I can think of a solution where I fire an event when the User obejct changes and this is caught my by control which then reinitialise's the binding but this seems less than ideal. Are there a solution to this issue, I feel it must be very common? Cheers, James c# wpf data-binding binding link|improve this question edited May 8 '09 at 10:16RichieHindle49.2k5103184 asked May 8 '09 at 10:10James65521133 78% accept rate.

May 8 '09 at 10:16 If the DataSource is changing, Setting the DataSource to the new object should automatically update bound controls. If a bound property is changing.. you need INotifyPropertyChanged – Gishu May 8 '09 at 12:30.

Most UI bindings already handle this via property notifications, in particular (for WPF) INotifyPropertyChanged. - i.e. If you are updating the UserId on a single instance: class User : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if(handler!

=null) handler(this, new PropertyChangdEventArgs( propertyName)); } private string userId; public string UserId { get {return userId;} set { if(userId! = value) { userId = value; OnPropertyChanged("UserId"); } } } } This should then update the binding automatically. If you are, instead, changing the actual user instance, then consider the same trick, but against whatever hosts the user: public User User { get {return user;} set { if(user!

= value) { user = value; OnPropertyChanged("User"); } } } And if you are binding to something's "User. UserId", then it should work.

As this will bubble up the changes. To your bound controls.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions