Technology Microsoft Software & solutions

WPF Tutorial Part 5: A First Introduction to WPF and XAML for Visual Basic

< Continued from page 4

Before WPF, an event was "owned" by the control raising it (Sender in the event subroutine argument list). Now that routed event can trigger event subroutine code from any element in the WPF visual tree, things have become a bit more confused. WPF gives you a way of nailing down exactly where some events are raised. It's called "attached events". In other WPF articles at About Visual Basic, I discuss "attached properties".


Attached events are similar.

One possible point of confusion is that not all events are declared as attached events. In fact, most are not. For example, you may see the Click event explained as an attached event in other articles. They use that example because it's the only one declared that way for ButtonBase (the actual object that raises the event) in the current version of WPF, VB.NET 2008/Framework 3.5. So the usefulness of attached event isn't as great as it might be ... yet.

Because you can find a lot on the web that documents how to use Click as an attached event, I decided to give you something different. Many systems use the F1 key to trigger Help. The simple example here uses the KeyUp event attached to a Grid container element to add text to a WPF TextBox when the F1 key is pressed. The application looks like this:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------


RadioButton, Displaying an Image

This app was written for a different purpose, but it can also illustrate an attachable event.

(If you want to read the "back story", read my blog about it here.) But if you're looking for a RadioButton example or an Image control example, this one might work for you as well. To download the project, Click Here.

The idea is that the name of a graphic can be entered into the

TextBox
and displayed in any of the four Image controls using the radio buttons. Click the OK button to display the graphic. If you forget the names of the graphics available, you can press F1 and get a display in the Label in the lower left corner.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------


The XAML code looks like this:


       ... etc. ...


The KeyUp event from the Keyboard object can be attached to the Grid, which makes it possible to code a single event subroutine that handles all keyboard input. For this simple example, this event subroutine does it:

Private Sub Grid_KeyUp( _
   ByVal sender As System.Object, _
   ByVal e As System.Windows.Input.KeyEventArgs)
   If e.Key = Key.F1 Then _
      ImageHelp.Content = _
      "LilAngel" & vbNewLine & _
      "PumpkinHead" & vbNewLine & _
      "LilDevil" & vbNewLine & _
      "TouristHound"
End Sub


This works the same way even if the KeyUp event isn't qualified by the Keyboard object in this case. This happens due to the way the WPF Input API works.

In Part 6 of the series, we return to the simple application that we coded in Part 1 that changed the opacity of a label component. In that application, we borrowed a Timer component from the System.Windows.Forms namespace to smoothly change the Opacity property. In the next article, we do it the Microsoft recommended way and learn about a really new part of .NET that has become more important in WPF: multithreading. Go to Part 6.
SHARE
RELATED POSTS on "Technology"
How to Stop Windows Genuine Notification Wizard
How to Stop Windows Genuine Notification Wizard
How to Use Windows XP Professional to Upgrade to Windows MCE 2005
How to Use Windows XP Professional to Upgrade to Windows MCE 2005
How do I Launch a Keyboard Macro?
How do I Launch a Keyboard Macro?
How to Tell If Windows Need Replacing
How to Tell If Windows Need Replacing
How to Uninstall a Malware Protector
How to Uninstall a Malware Protector
How to Get Rid of Newlines
How to Get Rid of Newlines
PC Diagnostic Help
PC Diagnostic Help
How to Disable Remote Devices in Group Policy
How to Disable Remote Devices in Group Policy
I Can't Use the Volume Buttons on My Headphones on My XP PC
I Can't Use the Volume Buttons on My Headphones on My XP PC
How to Restore Your PC to One Month Ago in Vista
How to Restore Your PC to One Month Ago in Vista
How to Unlock Volume Icons in the Notification Area of Windows Vista
How to Unlock Volume Icons in the Notification Area of Windows Vista
How to Fix Run Dll Errors
How to Fix Run Dll Errors
How To Run In Safe Mode In Windows Vista
How To Run In Safe Mode In Windows Vista
Microsoft Windows 2000 Registry Repair
Microsoft Windows 2000 Registry Repair
How to Unzip Files in Windows Vista Home Basic
How to Unzip Files in Windows Vista Home Basic
How to Enter Safe Mode on an Asus Eee PC
How to Enter Safe Mode on an Asus Eee PC
How to Open an HP Computer
How to Open an HP Computer
How to Identify What Network Adapters Are Installed on Windows Vista With No Manufacturer Listed
How to Identify What Network Adapters Are Installed on Windows Vista With No Manufacturer Listed
How to Fix Virtual Memory That Is Running Low
How to Fix Virtual Memory That Is Running Low
The Windows Update Won't Work After Installing XP SP3
The Windows Update Won't Work After Installing XP SP3
How to Access the BIOS Menu
How to Access the BIOS Menu
List of Computer Database Registry Programs
List of Computer Database Registry Programs
How to Boot a Computer to VMware
How to Boot a Computer to VMware
How to Raise Data Recovery for XFS
How to Raise Data Recovery for XFS
How to Clear the Windows ARP Cache
How to Clear the Windows ARP Cache

Leave Your Reply

*