Interact with this window
In the application class we can add code to hold references to important windows so that one window can access another. For example, suppose you want to track all the document windows that your application uses, you can create a specialized collection in your custom application class. The following is an example of saving a set of custom window objects using a generic list collection. In this example, each document window is represented by an instance of a class named document:
Public Partial class app:application{ privatenew list<document>(); Public List<document> documents { getreturn documents;} Set {documents = value;}} }
View CodeNow, when creating a new document, just remember to add it to the Documents collection. The following is an event handler that responds to a button click event, creates a new document and adds it to the Documents collection, as well, You can also respond to events such as window.loaded in the document class to ensure that the document object is always registered in the Documents collection when it is created.
Private void Cmdcreate_click (object sender, RoutedEventArgs e) { new Document (); this; Doc. Show (); (APP) (application.current). Documents.Add (DOC);}
View CodeThe collection can now be used anywhere else in the code to traverse all documents, and in this example the document class contains a custom SetContent () method for updating the display:
Private void Cmdupdate_click (object sender, RoutedEventArgs e) { foreach in (APP) (application.current). Documents) { doc. SetContent ("" ". " ); } }
View Code