This pattern gives you the flexibility to change where your database code is located, replace the
update button with a different control, and wire several controls to the same process, all without limiting
your ability to change the user interface later on. If you want a simpler way to deal with actions that can be
triggered from several different places in a user interface (toolbar buttons, menu commands, and so on),
you??™ll want to add the WPF command feature that??™s described in Chapter 10.
CHAPTER 6 n DEPENDENCY PROPERTIES AND ROUTED EVENTS 154
You can also connect an event with code. Here??™s the code equivalent of the XAML markup
shown previously:
AddHandler img.MouseUp, AddressOf img_MouseUp
The code approach is useful if you need to dynamically create a control and attach an
event handler at some point during the lifetime of your window. By comparison, the events
you hook up in XAML are always attached when the window object is first instantiated. The
code approach also allows you to keep your XAML simpler and more streamlined, which is
perfect if you plan to share it with nonprogrammers, such as a design artist. The drawback is
a significant amount of boilerplate code that will clutter up your code files.
The previous code approach relies on the event wrapper, which calls the UIElement.
Pages:
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314