Click here to Skip to main content
15,902,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need help with using INotifyPropertyChanged.

Im working on a Silverlight Application, which has a login window (which is a Silverlight Child window) and I want to access the user credentials entered in the login window from the MainPage (that is the Parent window). I know i need to do it with INotifyPropertyChanged. But I am not able to figure out how to do it. Can somebody give me an example for doing it?
Posted

@Aswin
I've added a new answer, as I can format it properly

OK. A simple way to do this is to create a model for the password page, typically it would have a property Username and Password, Lets call the type LoginCredentials, it would implement INotifyPropertyChanged. You then set the DataContext of the Login Window to a new LoginCredentials object in you can do this in "code-behind". You then bind the Text Properties of the Username and Password boxes to the equivalent properties in your DataContext.

See here for an article on Data Binding:
Moving Toward WPF Data Binding One Step at a Time[^], just look at "Version 4 – Binding in XAML"



I wasn't clear about "Interrogate the object at all", sorry, I was pretty tired when I replied, so my bad. The Main winow will have access to it's child Login Windows DataContext You can cast this to LoginCredentials and access the Username and Password properties you added.

Finally I'd add this: IMO, all the windows should have their own object model controlling behaviour, and have properties bound up in XAML. The code-behind is really just to support the events, etc. The models can be built up so that the main page model has a login credentials property. One caveat if you do this is that the password will be available in memory, so you should do something about this. I'd just keep the username in the Login model, store the password until the user logs in (the login model should be responsible for this), then keep a bool logged in status or some-such. That way you get good separation of concerns, and the code actually becomes clearer.

Hope this [rather long-winded] reply helps!
 
Share this answer
 
Comments
Aswin Ramakrishnan 26-Jul-10 10:38am    
Reason for my vote of 5
That works. Thanks
INotiftyPropertyChanged isn't really what you need, its main purpose is to allow the DataContext's object to alert the GUI that its property i changed in a binding scenario.
Once the data is entered, the main page should interrogate the object in the DataContext and get the values from that. A sample would take up a lot of space.

This all assumes you have bound up a model to the GUI. It would be easier to help if you gave some code please.
 
Share this answer
 
v2
Comments
Aswin Ramakrishnan 23-Jul-10 17:48pm    
@Keith

I have a textblock in the MainPage which just shows a Welcome message to the logged in user with the user's friendly name / user name. So what I am probably looking at is to change the text property of the Textblock in the main page once the user enters the login information and logs in from the child window.

I also tried using Web Service which gets data from the child window and sends it to another method in the web service which gives it to the MainPage. But it didnt work out.

One of my friends asked me to use Data Binding and INotifyPropertyChanged. I was looking online for stuff explaining INotifyPropertyChanged. But I find it difficult to interpret.

Also, What do you mean exactly by interrogating the object. Somehting like DependencyObject / DependencyProperty?
You don't need INotifyPropertyChanged. Just create a static class that holds the data, set the data from the main page when the user logs in, and you can then refer to it from any point in the application.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900