You
might also find yourself reviewing XAML to get a better idea of how a window works.
Using Types from Other Namespaces
So far, you??™ve seen how to create a basic user interface in XAML using the classes that are a
part of WPF. However, XAML is designed as an all-purpose way to instantiate .NET objects,
including ones that are in other non-WPF namespaces and those you create yourself.
It might seem odd to consider creating objects that aren??™t designed for onscreen display
in a XAML window, but there are a number of scenarios where it makes sense. One example is
when you use data binding and you want to draw information from another object to display
in a control. Another example is if you want to set the property of a WPF object using a non-
WPF object.
For example, you can fill a WPF ListBox with data objects. The ListBox will call the
ToString() method to get the text to display for each item in the list. (Or for an even better list
you can create a data template that extracts multiple pieces of information and formats them
appropriately. This technique is described in Chapter 17.)
In order to use a class that isn??™t defined in one of the WPF namespaces, you need to map
the .NET namespace to an XML namespace. XAML has a special syntax for doing this, which
looks like this:
xmlns:Prefix="clr-namespace:Namespace;assembly=AssemblyName"
Typically, you??™ll place this namespace mapping in the root element of your XAML document,
right after the attributes that declare the WPF and XAML namespaces.
Pages:
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154