I'm Keyvan Nayyeri, a 25 years old Ph.D. student at
the Computer Science department of
the University of Texas at San Antonio.
I'm also
a Software Architect and Developer and previously held a B.Sc.
degree in Applied Mathematics.
This is my blog where I publish content about various topics specifically Programming Languages and Compilers, Software
Engineering, and Programming.
Fahrenheit Marketing is a top-dog Austin Web Design firm offering a complete portfolio of online services.
So far I've discussed about basics of creating a custom WPF control and how to add properties to it. In this post I want to write about adding events to a custom WPF control.
Adding events to custom controls is very similar to adding properties to them in the way that we register them within our control's static constructor. We use RoutedEvents to declare our events. As you may know there are three types of RoutedEvents available in WPF (Bubble, Direct and Tunnel) that you can use in your controls (find more about Events in Windows Presentation Foundation from my article on ASP Alliance).
In order to add RoutedEvents to a custom control you can declare a public static RoutedEvent in your class and use EventManager.RegisterRoutedEvent() method to register these events. EventManager.RegisterRoutedEvent() gets four parameters: the string value of event name, routing strategy (Bubble, Direct or Tunnel), type of RoutedEventHandler and type of the custom control.
Following is a simple example that demonstrates this in action by adding a new routed event named Keyvan to a custom WPF control with Bubble routing strategy.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace WPFCustomControl
{
public class MyControl : ItemsControl
{
static MyControl()
{
EventManager.RegisterRoutedEvent("Keyvan", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(MyControl));
}
public static RoutedEvent KeyvanEvent;
public event RoutedEventHandler Keyvan
{
add { AddHandler(KeyvanEvent, value); }
remove { RemoveHandler(KeyvanEvent, value); }
}
protected virtual void OnKeyvan()
{
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = KeyvanEvent;
RaiseEvent(args);
}
}
}
Now playing: Modern Talking - Summer In December
Fahrenheit Marketing is your resource for Search Engine Optimization in Austin.
DotNetKicks.com
May 08, 2007 11:16 AM
#
Keyvan Nayyeri
May 15, 2007 11:08 AM
#
Abhinav
Sep 03, 2007 11:34 PM
#
Dusty
Jan 25, 2008 1:32 PM
#
Just to let you know, there's a bug in the code you provide. Line 13:
EventManager.RegisterRoutedEvent("Keyvan",
Should be:
KeyvanEvent = EventManager.RegisterRoutedEvent("Keyvan",
With the present code you'll get an ArgumentNullException when trying to add an event handler.
rafael
Nov 06, 2009 9:02 AM
#
shohreh
Aug 01, 2010 6:55 AM
#
you've refrenced two link in the first of this page about "basics of creating a custom WPF control " and "how to add properties to it".but both of them are disable.
Leave a Comment