) XAML parsers have a clever way of handling this. You can set either the XAML Name
property (using the x: prefix) or the Name property that belongs to the actual element (by
leaving out the prefix). Either way, the result is the same??”the name you specify is used in the
automatically generated code file and it??™s used to set the Name property.
That means the following markup is equivalent to what you??™ve already seen:
This bit of magic only works if the class that includes the Name property decorates itself
with the RuntimeNameProperty attribute. The RuntimeNameProperty indicates which property
should be treated as the name for instances of that type. (Obviously, it??™s usually the
property that??™s named Name.) The FrameworkElement class includes the RuntimeNameProperty
attribute, so there??™s no problem.
nTip In a traditional Windows Forms application, every control has a name. In a WPF application, there??™s
no such requirement. However, if you create a window by dragging and dropping elements onto the Visual
Studio design surface, each element will be given an automatically generated name. This is simply a convenience.
If you don??™t want to interact with an element in your code, you??™re free to remove its Name attribute
from the markup. The examples in this book usually omit element names when they aren??™t needed, which
makes the markup more concise.
Pages:
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129