Why don't my Silverlight UIElements have OnPreview events?

I am creating a custom Silverlight UserControl that needs to listen for events using Preview / Tunneling, but for some reason the compiler tells me they are not recognized or available.

For example, I can add an event handler to MouseLeftButtonDown

, but not PreviewMouseLeftButtonDown

. It doesn't make sense because according to Microsoft ( http://msdn.microsoft.com/en-us/library/system.windows.uielement_members(v=VS.100).aspx ) all UIElemen

ts must have pre-events attached.

Any ideas as to why this is happening? I am using Visual Studio 2010 Trial, Blend 4 RC and .Net 4 if that matters.

+2


a source to share


2 answers


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

If you're trying to create a control that works with both WPF and Silverlight, you'll need to take a different approach. Depending on what you are trying to do, you can accomplish what you want by compiling a code handler and specifying that you want to handle 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
);

      

+3


a source


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



The Silverlight UIElement Help does not show these events because they do not exist in Silverlight.

+1


a source







All Articles