Although XAML doesn??™t
allow it, you can use an overloaded version of the SetValue() method in code to attach a value
for any dependency property, even if that property isn??™t defined as an attached property. For
example, the following code is perfectly legitimate:
Dim comboBox As New ComboBox()
...
comboBox.SetValue(PasswordBox.PasswordCharProperty, "*")
Here, a value for the PasswordBox.PasswordChar property is set for a ComboBox object,
even though PasswordBox.PasswordCharProperty is registered as an ordinary dependency
property, not an attached property. This action won??™t change the way the ComboBox works??”
after all, the code inside the ComboBox won??™t look for the value of a property that it doesn??™t
know exists??”but you could act upon the PasswordChar value in your own code.
Although rarely used, this quirk provides some more insight into the way the WPF property
system works, and it demonstrates its remarkable extensibility. It also shows that even
though attached properties are registered with a different method than normal dependency
properties, in the eyes of WPF there??™s no real distinction. The only difference is what the XAML
parser allows. Unless you register your property as an attached property, you won??™t be able to
set it in on other elements in your markup.
Pages:
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303