Disable Context Menu In Silverlight 3 Application

No.of Views1349
Bookmarked0 times
Downloads 
Votes0
By  dpatra   On  16 Feb 2010 00:02:00
Tag : Silver Light and XAML , How to
Disable Context Menu In Silverlight 3 Application
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article we will see how can we disable the ContextMenu when you Right Click on Silverlight Page .

Crating Silverlight Project

Fire up Visual Studio 2008 and create a Silverlight Application. Name it as DisableContextMenuSL3.

Image Loading....

Default Context Menu


In any Silverlight Application when you right click on it you see a context menu containing Item “Silverlight”. Like following:

Image Loading...

And when you click on it you find the Silverlight Configuration Dialog popping up.

Image Loading....

Disabling Context Menu

What if we don’t want the Context Menu and don’t want to see the Configuration dialog. We will see how we can do that.
1) First of all open the “DisableContextMenuSL3TestPage.aspx” page and find the object tag where Silverlight Plug in is being hosted.

{codecitation class="brush: xml; gutter: true;" width="650px"}

<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/DisableContextMenuSL3.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40624.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object>
<iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
</div>

{/codecitation}

2) Now add a param tag with name=”Windowless” and value=”true”. By default its value is false.

{codecitation class="brush: xml; gutter: true;" width="650px"}

<param name="Windowless" value="true" />

{/codecitation}
3) Now open “MainPage.xaml.cs”. Add the Namespace “System.Windows.Browser”

{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System.Windows.Browser;

{/codecitation}

4) Add a class inside the MainPage.cs name it as ContextMenuInterceptor.
5) Add method OnContextMenu with arguments object and HtmlEventArgs.
6) Add default constructor with the following code:

{codecitation class="brush: csharp; gutter: true;" width="650px"}

HtmlPage.Document.AttachEvent("oncontextmenu", this.OnContextMenu);

{/codecitation}


The full class definition looks like the following.



{codecitation class="brush: csharp; gutter: true;" width="650px"}

public class ContextMenuInterceptor
{
public ContextMenuInterceptor()
{
HtmlPage.Document.AttachEvent("oncontextmenu", this.OnContextMenu);
}

private void OnContextMenu(object sender, HtmlEventArgs e)
{
e.PreventDefault();
}
}


{/codecitation}


7) Now in MainPage class create an instance of the Class you just created.

{codecitation class="brush: csharp; gutter: true;" width="650px"}

ContextMenuInterceptor context = new ContextMenuInterceptor();

{/codecitation}


That’s it Run your application and try to right click on the page. You won’t get the Context Menu for Silverlight.enjoy Coding.

Thank you


About the Author


Diptimaya Patra

Description :I am a Master in Computer Application (MCA) from SRM University, Chennai. I am MCTS in ASP.Net Web Development, and MOSS 2007 Administration. I have extreme exposure to Microsoft Technologies in recent times like Silverlight 2, Silverlight 3. I am from Cuttack, Orissa. You can reach me using this mail (diptimaya.patra@gmail.com). Currently I am working as a Software Engineer in UST Global Inc in Trivandrum Center.

Occupation :Software Engineer
Company : UST Global.
Location : India
Follow me at twitter : http://twitter.com/dpatra


 
Sign Up to vote for this article
 
About Author
 
dpatra
Occupation-Not Provided
Company-Not Provided
Member Type-Expert
Location-Not Provided
Joined date-13 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top