views:

36

answers:

2

I'm building a custom Silverlight UserControl which needs to listen to events using Preview/Tunneling, but for some reason the compiler is telling me they are not recognized or accessible.

For example, I can add an event handler to MouseLeftButtonDown, but not PreviewMouseLeftButtonDown. This doesn't make sense because according to Microsoft (http://msdn.microsoft.com/en-us/library/system.windows.uielement_members(v=VS.100).aspx) all UIElements should have Preview events attached.

Any ideas as to why this is happening? I'm using Visual Studio 2010 Trial, Blend 4 RC and .Net 4, if that makes a difference.

+1  A: 

You're looking at the help for WPF, not Silverlight. Silverlight is (mostly) a subset of WPF, and much of the functionality is missing.

The Silverlight UIElement help does not show those events, as they do not exist in Silverlight.

Reed Copsey
Interesting, thank you!
Matt.M
+2  A: 

Silverlight does not support preview events nor does it support routed events (bubbling/tunneling) except for a few core events.

If you are trying to create a control that works with both WPF and Silverlight, you will need to take a different approach. Depending on what you're trying to do, you may be able to accomplish what you want by rigging up a handler in code and specifying that you want handled events too.

// the last parameter indicates we want to receive events that
// were marked as e.Handled = true by other listeners
// this type of event handler can only be done in code
myUserControl.AddHandler(
    UIElement.MouseLeftButtonDownEvent,
    OnMouseLeftButtonDown,
    true
);
Josh Einstein
Perfect! Exactly what I needed, thank you for going the extra mile and helping me solve my problem.
Matt.M