Click here to Skip to main content
Click here to Skip to main content

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

By , 10 Feb 2011
 

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:

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:

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)

About the Author

Vasudevan Kannan
Software Developer (Senior) Venteq Inc
United States United States
Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGoodmemberseu.09300621 May '13 - 16:15 
Thumbs Up | :thumbsup: yeah , you solved a question for me , thank u
GeneralRe: GoodmemberVasudevan Kannan21 May '13 - 22:40 
Thanks .Glad it helped some one
QuestionNot Working with Child windowmemberanandan249 Feb '12 - 20:44 
Hi Vasudevan,
 
Its working fine for silverlight user control pages.but when i open the child window i can able to do the right click within the child window only.please let me know,what will be the reason for this?
AnswerRe: Not Working with Child windowmemberVasudevan Kannan10 Feb '12 - 5:56 
Hi,
 
Could you please send me the scaled down version of your project or a code sample to reproduce the problem you are talking about.
 
Thanks.
 
Best Regards,
Vasudevan Kannan
Generalgood article ,Vasudevanmembervikas amin15 Feb '11 - 9:10 
good article ,Vasudevan
cheers for your first article on CP
GeneralRe: good article ,VasudevanmemberVasudevan Kannan15 Feb '11 - 9:14 
Thanks Vikas Smile | :)
GeneralYou may include some more...mvpKunal_Chowdhury9 Feb '11 - 6:01 
as I said earlier, it will be better if you could include more snaps and sample solution. This will help your reader to understand properly before digging into the code.

Please Vote for the Articles and/or Posts that you like.
Kunal Chowdhury (Microsoft MVP (Silverlight) | CodeProject MVP | Follow My Blog | Follow Silverlight-Zone | Follow Me @ Twitter)

GeneralRe: You may include some more...memberVasudevan Kannan9 Feb '11 - 6:33 
Thanks for your suggestion .Added as sample project and some images for the concept
GeneralRe: You may include some more...mvpKunal_Chowdhury9 Feb '11 - 7:16 
Looks little cool now. Thumbs Up | :thumbsup:

Please Vote for the Articles and/or Posts that you like.
Kunal Chowdhury (Microsoft MVP (Silverlight) | CodeProject MVP | Follow My Blog | Follow Silverlight-Zone | Follow Me @ Twitter)

QuestionRe: You may include some more...memberVasudevan Kannan9 Feb '11 - 8:07 
Thanks ,any more changes required for this to be approved
AnswerRe: You may include some more...mvpKunal_Chowdhury9 Feb '11 - 15:20 
Someone already approved it.

Please Vote for the Articles and/or Posts that you like.
Kunal Chowdhury (Microsoft MVP (Silverlight) | CodeProject MVP | Follow My Blog | Follow Silverlight-Zone | Follow Me @ Twitter)

QuestionRe: You may include some more...memberVasudevan Kannan9 Feb '11 - 16:15 
Thanks kunal, will my article automatically move in to the regular section or will still
 
remain as an unedited contibution
AnswerRe: You may include some more...mvpKunal_Chowdhury9 Feb '11 - 16:21 
Hello Vasu,
 
I can see that, your article is public. So, no need to worry about it. As long as you have placed proper tag "Silverlight" for your article, it will go to the correct section. It will show unedited because you marked it like so but no need to worry.
 
Let me know, if I can help you in any ways.
 
BTW, Check this: http://bit.ly/gy96bh[^]

Please Vote for the Articles and/or Posts that you like.
Kunal Chowdhury (Microsoft MVP (Silverlight) | CodeProject MVP | Follow My Blog | Follow Silverlight-Zone | Follow Me @ Twitter)

GeneralRe: You may include some more...memberVasudevan 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...mvpKunal_Chowdhury9 Feb '11 - 18:01 
Thumbs Up | :thumbsup: Waiting for your next article. Cheers...

Please Vote for the Articles and/or Posts that you like.
Kunal Chowdhury (Microsoft MVP (Silverlight) | CodeProject MVP | Follow My Blog | Follow Silverlight-Zone | Follow Me @ Twitter)

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 10 Feb 2011
Article Copyright 2011 by Vasudevan Kannan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid