Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#

How to Disable Silverlight Configuration Dialog which Appears on Right Click in a MVVM Silverlight 4.0 Application

Rate me:
Please Sign up or sign in to vote.
4.25/5 (3 votes)
10 Feb 2011Ms-PL3 min read 48.8K   13   15
The purpose of this article is to show how the default dialog displayed by the Silverlight plugin can be prevented from being shown to the user on right clicking on a Silverlight application.

Introduction

Recently in one of the projects I was working on, we had the need to disable the Silverlight configuration dialog box which appears automatically when the user clicks anywhere on the Silverlight application as shown below: 

Microsoft_silverlight_configuration.JPG

The Business Problem

The Silverlight configuration dialog has got a lot of useful functionality and the ability to quickly uninstall the application. However most of the time, you would want to prevent the business users from knowing the details of the implementation. There is also a possibility that the business users could change a few settings inadvertently and cause the application from working or worse they could uninstall the application without knowing to install the application back again.

The image bellows shows a test solution which shows the right click menu which appears by default.

RightclickInsideApplication.JPG

Solution

One possible approach for solving this problem would be to use JavaScript to disable the right click at the plugin level. This approach however would disable the right click event for the entire application and will not work in out of browser mode. The solution presented in this article uses the right click event handler exposed in the Silverlight 4 version and will not work in the previous versions of Silverlight. The solution is to add an event handler to the mouse right button down event in the application startup method. In the event handler, we set the ishandled property to true. This essentially prevents the event from bubbling up all the way to the Silverlight plugin. The source code for the same is shown below:

C#
private void Application_Startup(object sender, StartupEventArgs e)
        {           
                Application.Current.RootVisual.MouseRightButtonDown += 
		new System.Windows.Input.MouseButtonEventHandler
				(RootVisual_MouseRightButtonDown);  
        }

        void RootVisual_MouseRightButtonDown
		(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

	private void Application_Exit(object sender, EventArgs e)
        {
		Application.Current.RootVisual.MouseRightButtonDown -= 
			new System.Windows.Input.MouseButtonEventHandler
				(RootVisual_MouseRightButtonDown);
        }

It appears to be a easy workaround. However, we still have a problem. It can be observed that when the datepicker popup window is open, the user could right click into the popup control and get the configuration dialog.

The problem is shown below:

RightClickInsideTimePicker.JPG

After spending some time, I got the information from the MSDN documentation that certain objects in Silverlight participate in a relation with the primary tree conceptually like an overlay over the main visuals. These objects are not part of the usual parent-child relationships that connect all tree elements to the visual root. This is the case for any displayed Popup or ToolTip. If you want to handle routed events from a Popup or ToolTip, you should place the handlers on specific UI elements that are within the Popup or ToolTip and not the Popup or ToolTip elements themselves. You should not rely on routing inside any compositing that is performed for Popup or ToolTip content. This is because event routing for routed events works only along the main visual tree. Popup or ToolTip is not considered a parent of subsidiary UI elements and never receives the routed event, even if it is trying to use something like the Popup default background as the capture area for input events.

So in order to solve the problem completely, any control that uses a popup like date picker must be overridden to use a custom control that explicitly overrides the right click behaviour of the popup and sets the right click event as handled. An example for a custom time picker control is shown below:

C#
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace Controls
{
    public class CustomTimePicker:TimePicker
    {
        private Canvas _canvas = null;

        private Popup _p = null;

        internal Canvas PopupCanvas
        {
            get { return _canvas; }

            set
            {
                if (_canvas != null)
                {
                    _canvas.MouseRightButtonDown -= 
			new MouseButtonEventHandler(PopupCanvas_MouseRightButtonDown);
                }

                _canvas = value;

                if (_canvas != null)
                {
                    _canvas.MouseRightButtonDown += 
			new MouseButtonEventHandler(PopupCanvas_MouseRightButtonDown);
                }
            }
        }

        internal Popup TimepickerPopup
        {
            get { return _p; }

            set
            {
                _p = value;

            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            TimepickerPopup = GetTemplateChild("Popup") as Popup;

            if (TimepickerPopup != null)
            {
                PopupCanvas = TimepickerPopup.Child as Canvas;
            }
            if (PopupCanvas != null)
            {
                PopupCanvas.MouseRightButtonDown += new MouseButtonEventHandler
					(PopupCanvas_MouseRightButtonDown);
            }
        }

        void PopupCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
    }
}	

Reference

History

  • Initial draft version V1.0 (08-Feb-2011)

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) Venteq Inc
United States United States
I am Vasudevan Kannan ,currently working on a project for a reputed health care organization in Greater Salt Lake Area.

I got addicted to computers and programming from my child hood.My key skills include WPF and Silverlight and enjoy reading and writing technical articles during my free time

Comments and Discussions

 
GeneralGood Pin
seu.09300621-May-13 16:15
seu.09300621-May-13 16:15 
GeneralRe: Good Pin
Vasudevan Kannan21-May-13 22:40
Vasudevan Kannan21-May-13 22:40 
QuestionNot Working with Child window Pin
anandan249-Feb-12 20:44
anandan249-Feb-12 20:44 
AnswerRe: Not Working with Child window Pin
Vasudevan Kannan10-Feb-12 5:56
Vasudevan Kannan10-Feb-12 5:56 
Generalgood article ,Vasudevan Pin
vikas amin15-Feb-11 9:10
vikas amin15-Feb-11 9:10 
GeneralRe: good article ,Vasudevan Pin
Vasudevan Kannan15-Feb-11 9:14
Vasudevan Kannan15-Feb-11 9:14 
GeneralYou may include some more... Pin
Kunal Chowdhury «IN»9-Feb-11 6:01
professionalKunal Chowdhury «IN»9-Feb-11 6:01 
GeneralRe: You may include some more... Pin
Vasudevan Kannan9-Feb-11 6:33
Vasudevan Kannan9-Feb-11 6:33 
GeneralRe: You may include some more... Pin
Kunal Chowdhury «IN»9-Feb-11 7:16
professionalKunal Chowdhury «IN»9-Feb-11 7:16 
QuestionRe: You may include some more... Pin
Vasudevan Kannan9-Feb-11 8:07
Vasudevan Kannan9-Feb-11 8:07 
AnswerRe: You may include some more... Pin
Kunal Chowdhury «IN»9-Feb-11 15:20
professionalKunal Chowdhury «IN»9-Feb-11 15:20 
QuestionRe: You may include some more... Pin
Vasudevan Kannan9-Feb-11 16:15
Vasudevan Kannan9-Feb-11 16:15 
AnswerRe: You may include some more... Pin
Kunal Chowdhury «IN»9-Feb-11 16:21
professionalKunal Chowdhury «IN»9-Feb-11 16:21 
GeneralRe: You may include some more... Pin
Vasudevan Kannan9-Feb-11 16:37
Vasudevan Kannan9-Feb-11 16:37 
Thanks Kunal, was worried a bit as this is my first article.

Thanks for providing a link to my article,

Hope fully we shall meet with my next article
GeneralRe: You may include some more... Pin
Kunal Chowdhury «IN»9-Feb-11 18:01
professionalKunal Chowdhury «IN»9-Feb-11 18:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.