You may have noticed that right clicking on a Silverlight application brings up the following context menu and configuration dialog:
Context Menu:
Configuration Dialog:
So what if you want to use right click in your application? While right click functionality is not currently supported in Silverlight, there is a work-around. I will step you through how to intercept the right click event, process it and display your own content instead of the Silverlight dialog.
Step 1. To start, let’s add a <TextBlock> control to our Page.xaml to track the status of the right click:
<UserControl x:Class="SilverlightApplication15.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="red">
<TextBlock x:Name="MyField">Right click please.</TextBlock>
</Grid>
</UserControl>
Step 2. Next, we need to set the Silverlight control to be windowless. Open the web page (i.e. default.aspx) that contains the Silverlight control and add the tag Windowless=”true” to it. Example:
<asp:Silverlight ID="Xaml1" runat="server" Windowless="true" Source="~/ClientBin/SilverlightApplication15.xap" MinimumVersion="2.0.30523" Width="100%" Height="100%" />
Step 3: Finally, let’s take a look at the code we add to Page.xaml.cs.
Page.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
namespace SilverlightApplication15
{
public partial class Page : UserControl
ContextMenuInterceptor _cmi = null;
public Page()
InitializeComponent();
_cmi = new ContextMenuInterceptor(MyField);
}
public class ContextMenuInterceptor
TextBlock TextField;
public ContextMenuInterceptor(TextBlock textField)
TextField = textField;
HtmlPage.Document.AttachEvent("oncontextmenu", this.OnContextMenu);
private void OnContextMenu(object sender, HtmlEventArgs e)
TextField.Text = "Right Clicked Blocked at "+e.OffsetX+","+e.OffsetY;
e.PreventDefault();
Thank you, --Mike Snow Subscribe in a reader
PingBack from http://www.tmao.info/silverlight-tip-of-the-day-14-%e2%80%93-how-to-right-click-on-a-silverlight-application/
Voila un article sur lequel je viens de tomber. Le post explique comment intercepter le click droit et
silverlight中可以处理很多键盘和鼠标事件,然而遗憾的是这些事件并不包括鼠标右键事件:鼠标右键事件被Silverlight 自己的上下文菜单“Silverlight Configuration”占用;然而富交互程序大都希望使用鼠标右键弹出上下文菜单或实现快捷操作。 幸运的是,前辈大牛们早已解决了这个问题,本文参考已有的解决方案,给出了详细的实现步骤。