Section 8.2. Common Event Reference


8.2. Common Event Reference

Elements deriving from UIElement inherit a set of common events. Events are used in XAML to specify the codebehind handler that will be executed when the specified event is raised. All events can be assigned a codebehind handler using the following syntax:

 <Element Name="ElementName" EventName="CodeBehindHandler" /> 

Elements specifying a codebehind handler must declare the Name attribute in order to be referenced in the codebehind class. Examples 8-2 and 8-3 demonstrate the event-handling code in C# and VisualBasic, respectively, which is executed when the mouse cursor enters or leaves the Button declared in Example 8-4.

Example 8-2. C# implementation of event handlers

 public partial class MouseEnterMouseLeave {     void MouseEnterHandler (object sender, MouseEventArgs e)     {         MyButton .Background=Brushes.Red;         MyButton .Content="Mouse is over me ";     }     void MouseLeaveHandler (object sender, MouseEventArgs e)     {         MyButton .Background=Brushes.White;         MyButton .Content="Mouse is not over me ";     } } 

Example 8-3. VisualBasic implementation of event handlers

 Partial Public Class MouseEnterMouseLeave     Sub MouseEnterHandler (ByVal sender as Object, ByVal e As MouseEventArgs         MyButton .Background = Brushes.Red         MyButton .Content = "Mouse is over me "     End Sub     Sub MouseLeaveHandler (ByVal sender As Object, ByVal e as MouseEventArgs         MyButton .Background = Brushes.White         MyButton .Content = "Mouse is not over me "     End Sub End Class 

Example 8-4. XAML declaration of event handlers for Button

 <StackPanel     xmlns="http://schemas.microsoft.com/winfx/avalon/2005"     xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"     x :     <Button         Content="Mouse is not over me "         MouseEnter="MouseEnterHandler "         MouseLeave="MouseLeaveHandler "         Name="MyButton " /> </StackPanel> 

The following events are common to all UIElement-derived elements. Events specific to the element are listed with the element description. Events are fully detailed in Chapter 12, along with a more thorough exploration of the event subsystem.

  • DragEnter

  • DragLeave

  • DragOver

  • Drop

  • GotFocus

  • IsVisibleChanged

  • IsEnableChanged

  • IsFocusChanged

  • KeyUp

  • KeyDown

  • LayoutUpdated

  • LostFocus

  • MouseEnter

  • MouseLeave

  • MouseMove

  • MouseLeftButtonDown

  • MouseLeftButtonUp

  • MouseRightButtonDown

  • MouseRightButtonUp

  • PreviewDragOver

  • PreviewDragEnter

  • PreviewDragLeave

  • PreviewDrop

  • PreviewKeyUp

  • PreviewKeyDown

  • PreviewMouseLeftButtonDown

  • PreviewMouseLeftButtonUp

  • PreviewMouseRightButtonDown

  • PreviewMouseRightButtonUp

  • PreviewMouseMove




XAML in a Nutshell
XAML in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596526733
EAN: 2147483647
Year: 2007
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net