Page view counter

Did You Know That... You can put events into your XAML or into the code behind?

There are two common idioms for creating events (and wiring up their handlers) for XAML objects.

One is to place the name of the event and the name of its handler in the XAML itself

<Canvas
 Canvas.Top="180"
 Canvas.Left="200"      
 MouseLeftButtonDown="onMouseDown"
 MouseLeftButtonUp="onMouseUp"
 MouseMove="onMouseMove">

The other is to create a link between a reference to the XAML object and its handler in the code-behind; typically in the handler for page load

this.button.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
this.button.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));
this.button.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));

The CreateDelegate method ensures that the event is correctly routed. This method is created for you by the Silverlight 1.0 template in Visual Studio.

Silverlight.createDelegate = function(instance, method)
{
    return function() 
    {
        return method.apply(instance, arguments);
    }
}
kick it on DotNetKicks.com
Published Tuesday, January 15, 2008 10:00 AM by jesseliberty
Filed under:

Comments

# You can wire event handlers in XAML or in the code behind

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Tuesday, January 15, 2008 11:35 AM by DotNetKicks.com

# Pages tagged "events"

Pingback from  Pages tagged "events"

Tuesday, January 15, 2008 3:26 PM by Pages tagged "events"

# Did You Know That... The Code Behind is Just... Code?

A very insightful reader sent me email that I&#39;m going to respond to here. To protect his identity

Sunday, February 03, 2008 1:34 PM by Blogs

# Tips of the Day: Setting properties in custom controls in 1.0

Got a nice international email today that said in part... ...things get more interesting it is a common

Tuesday, February 05, 2008 2:10 PM by Jesse Liberty - Silverlight Geek

# Did You Know That... The Code Behind is Just... Code?

A very insightful reader sent me email that I&#39;m going to respond to here. To protect his identity

Tuesday, February 05, 2008 2:43 PM by Jesse Liberty - Silverlight Geek

# Tips of the Day: Setting properties in custom controls in 1.0

Got a nice international email today that said in part... ...things get more interesting it is a common

Tuesday, February 05, 2008 3:08 PM by Jesse Liberty

# Tips of the Day: Setting properties in custom controls in 1.0

Got a nice international email today that said in part... ...things get more interesting it is a common

Wednesday, February 20, 2008 2:55 AM by Mirrored Blogs