XamlReader
The set of XamlReader.Load methods parse XAML, create the appropriate .NET objects, and return an instance of the root element.
Window window = null; using (FileStream fs = new FileStream("MyWindow.xaml", FileMode.Open, FileAccess.Read)) { // Get the root element, which we know is a Window window = (Window)XamlReader.Load(fs); } // Grab the OK button by walking the children (with hard-coded knowledge!) StackPanel panel = (StackPanel)window.Content; Button okButton = (Button)panel.Children[4];
If the structure of the window is not known, we can use name of the control to find the control like below.
Window window = null; using (FileStream fs = new FileStream(“MyWindow.xaml”, FileMode.Open, FileAccess.Read)) { // Get the root element, which we know is a Window window = (Window)XamlReader.Load(fs); } // Grab the OK button, knowing only its name Button okButton = (Button)window.FindName("okButton");
|
No responses found. Be the first to respond and make money from revenue sharing program.
|