Question from Silverlight forums: Custom Context menu and Mouse Right Click Event

22 באוקטובר 2007

2 תגובות


Is possible to create a context menu with Silverlight 1.1 and override or extend the currently context menu with Silverlight configuration?



First let's explain the problem further. Silverlight doesn't have an event to catch a mouse right click. Here's the official list of Mouse events in Silverlight (from the SDK):


And if we right click on a Silverlight application we'll get this normal context menu:


There are occasions where we would need to use our own context menu and not the default silverlight one.

So yes, it's completely possible to change Silverlight's ContextMenu.
The solution isn't one native to Silverlight, though it can be implemented almost completely either in SL 1.1 or SL 1.0.

The main point of our solution is using the Javascript "ContextMenu" event the belongs to Silverlight host DOM element and creating our own implementation of it. That's how we created custom Context Menu using Javascript back in the good old HTML days.

With Internet Explorer we can sign-up for the event using this syntax:

    <div id="SilverlightControlHost" class="silverlightHost" oncontextmenu="RTLRightClickFix();" >

        <script type="text/javascript">




With FireFox we'll need to use this Syntax:

if (window.addEventListener) {

    window.addEventListener('DOMContentLoaded', FirefoxLoad, true);



function FirefoxLoad() {

    document.getElementById('SilverlightControlHost').addEventListener('contextmenu', RTLRightClickFix, true);



Now, we've got the "RTLRightClickFix" method that'll get called on a mouse right click event!
We've basically just created Silverlight's missing "MouseRightClick" event.


Now that we've got control over the Right Click event we can show any "Menu" we'd like based on current mouse positions.

In SL 1.1 this is roughly the point we'll call a [ScriptableAttribute()] API and have it do most of the heavy lifting (showing a Context Menu Canvas based on current (X,Y) mouse positions).


The code for this solution and a live example can found at:



הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

2 תגובות

  1. Uriger8 בפברואר 2009 ב 10:35

    Great Post.
    The problem I have now is that this is not working when using different languages (french, Japanese etc..)
    Any ideas how to solve it?