New User?   Join Now     Login            Forgot Password?    
XamlQuery v1.2
Event Handling in XamlQuery
Posted on Aug-08-2010 (3330 hits)

Events in XamlQuery

The AddHandler and RemoveHandler methods of XamlQuery are used to attach and detach routed events to all the controls in ControlSet. In additional to these methods, XamlQuery defines an enumeration EventType for easier handling of events. Any method derived from Delegate class can be attached to any event, unless the signature of event-arguments match.

The following are the values of the enumeration constant EventType.

Events of controls extended from UIElement

  • GotFocus
  • LostFocus
  • KeyDown
  • KeyUp
  • MouseEnter
  • MouseLeave
  • MouseMove
  • LostMouseCapture
  • MouseLeftButtonDown
  • MouseLeftButtonUp
  • MouseWheel

Events of controls extended from FrameworkElement

  • BindingValidationError
  • Loaded
  • LayoutUpdated
  • SizeChanged

Events of controls extended from Control

  • IsEnabledChanged

Events of TextBox

  • TextChanged
  • TextSelectionChanged

Events of controls extended from Selector

  • SelectionChanged

XamlQuery defines a method for each of these events. These methods are used as shortcuts for Bind() and Trigger() methods.

Examples

There are several ways to bind a event handler to an event. The following example illustrates these.

ControlSet allShapes = XamlQuery.ByType<Rectangle>(MainCanvas);

//method-1
allShapes.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(delegate(object senderObject, MouseButtonEventArgs eventArgs)
{
    Rectangle rectangle = (Rectangle)senderObject;
    rectangle.SetValue(Shape.FillProperty, new SolidColorBrush(Colors.Green));
}));

//method-2
allShapes.Bind(EventType.MouseEnter, new MouseEventHandler(delegate(object senderObject, MouseEventArgs eventArgs)
{
    Rectangle rectangle = (Rectangle)senderObject;
    rectangle.SetValue(Shape.FillProperty, new SolidColorBrush(Colors.Green));
}));

//method-3
allShapes.Bind(EventType.MouseEnter, new MouseEventHandler(rectangle_MouseEnter));

//method-4
allShapes.MouseEnter(new MouseEventHandler(delegate(object senderObject, MouseEventArgs eventArgs)
{
    Rectangle rectangle = (Rectangle)senderObject;
    rectangle.SetValue(Shape.FillProperty, new SolidColorBrush(Colors.Green));
}));

//method-5
allShapes.ForEach(delegate(DependencyObject child)
{
    ((UIElement)child).MouseEnter += rectangle_MouseEnter;
});

The rectangle_MouseEnter used in this example is a event-handler method, defined below.

void rectangle_MouseEnter(object sender, MouseEventArgs e)
{
    Rectangle rectangle = (Rectangle)sender;
    rectangle.SetValue(Shape.FillProperty, new SolidColorBrush(Colors.Green));
}

About About      Terms Terms      Contact Contact
codeding on Facebook Facebook      codeding on Twitter Twitter