Another alternative is to use the FrameworkElement.FindName() method. In this example,
the root element is a DockPanel object. Like all the controls in a WPF window, DockPanel
derives from FrameworkElement. That means you can replace this code:
button1 = CType( _
LogicalTreeHelper.FindLogicalNode(rootElement, "button1"), _
Button)
with this equivalent approach:
Dim frameworkElement As FrameworkElement = CType(rootElement, FrameworkElement)
button1 = CType(frameworkElement.FindName("button1"), Button)
Obviously, loading XAML dynamically won??™t be as efficient as compiling the XAML to
BAML and then loading the BAML at runtime, particularly if your user interface is complex.
However, it opens up a number of possibilities for building dynamic user interfaces.
For example, you could create an all-purpose survey application that reads a form file
from a web service and then displays the corresponding survey controls (labels, text boxes,
check boxes, and so on). The form file would be an ordinary XML document with WPF tags,
which you load into an existing form using the XamlReader. To collect the results once the survey
is filled out, you simply need to enumerate over all the input controls and grab their
content.
Code and Compiled XAML
You??™ve already seen the most common way to use XAML with the eight ball example shown in
Figure 2-1 and dissected throughout this chapter.
Pages:
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163