XAML Reader
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];
With reference to button, we can set additonal properties, attch event handlers and so on.
If the structure of window is not known, we can write like below by usig names of controls.
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.
|