I set up two Windows in my WPF C# Blend project. Now what I would like to know (and I have trouble finding some clear documentation) is how to run both windows on application startup and in what way I can pass code from window to window.
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the
app.xamlfile for the solution, it specifies which window to run upon startup. A quick solution to open the other one is to tack on an event handler to the startup window’sLoadedevent which opens the second window.However, that’s not too scalable of a solution if this is part of a larger project. Having a separate class which can open each window, then neither window needs to know about the other.
As for passing data between them, using events can offer a more loosely-coupled solution. I’d push for a more MVVM (Model-View-ViewModel) architecture, then let each of the ViewModels raise events that the other can respond to. You can declare your own subclass of
EventArgswhich would supply the information which needs to be passed.Update
Sorry for the delay in response. Simply, to have one Window share data with another Window, the receiver must have a way to receive that data. Defining a public property in the receiver will allow the sender to specify the data with a simple property call. By default, a Window’s controls are internal, so you can access them within the same assembly, but that’s not the best way to do it.
WPF has a really rich binding infrastructure that you should be taking advantage of. Do do this, your object which is providing data to the Window needs to implement the
INotifyPropertyChangedinterface. This will alert the UI that data has changed, and the binding should update the target with the changed data. The MSDN page describing the interface as well as a sample implementation can be found here.When you implement that interface, that will expose an event (
PropertyChanged) which will fire when data has been changed. The object providing data to the other window can register an event handler to listen for these changes, and then it will have the updated value.Here’s an example implementation of a simple class with a FirstName and LastName property.
You can see that there’s some code duplication in here–that’s often refactored into a base ViewModel class. You’ll see that this exposes the
PropertyChangedevent. Attach an event handler to it, and in thePropertyChangedEventArgsobject the handler receives, thePropertyNameproperty will contain the name of the property that was changed (the same as the string passed to the constructor in each of the setters above). Thesenderparameter will be a reference to the object itself. Cast it to the correct type, and you’ll have access to the properties.Hopefully that gets you a start. I wrote a very contrived sample that I can upload somewhere if you’d like to see it. It opens 2 windows, then you can see that typing in one window causes the typed text to appear in the other one, and vice versa.