Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » Silverlight » Right click event in Silverlight 4

Right click event in Silverlight 4

In this article you will learn how to use Right click event in Silverlight 4.

Author Rank :
Page Views : 3836
Downloads : 59
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
SilverlightApplicationin4.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


In the previous versions of Silverlight, we were not able to code for mouse right click event.

When we right click on the silverlight page we are provided with the silverlight dialogbox like this:

1.gif

2.gif

But now in the latest version of Silverlight 4, we are able to code for right click events. Let's see a simple example to demonstrate the right click event in silverlight 4.

Here I am going to display a contextmenu in right click of a silverlight control.

The page design includes one stack panel with three buttons.

<Canvas x:Name="LayoutRoot" Background="#FFF90202">
          <StackPanel x:Name="sp1" Height="93" Canvas.Left="268" Canvas.Top="164" Width="159" Visibility="Collapsed">
                   <Button x:Name="button1" Content="Button" Height="31" Width="159" Template="{StaticResource ButtonControlTemplate1}" HorizontalAlignment="Right" Margin="0" Click="button1_MouseLeftButtonDown" Cursor="Hand" MouseEnter="button1_mouseenter"/>
                   <Button x:Name="button2" Content="Button" Height="31" Width="159" Template="{StaticResource ButtonControlTemplate2}" HorizontalAlignment="Right" Margin="0" Cursor="Hand" Click="button2_MouseLeftButtonDown"/>
                   <Button x:Name="button3" Content="Button" Height="31" Width="159" Template="{StaticResource ButtonControlTemplate3}" HorizontalAlignment="Right" Margin="0"
Cursor="Hand" Click="b3_click"/>
          </StackPanel>
</
Canvas>

I have updated the button control with custom styling. Here is the XAML code of first button:

<ControlTemplate x:Key="ButtonControlTemplate1" TargetType="Button">
          <Grid>
                   <VisualStateManager.VisualStateGroups>
                             <VisualStateGroup x:Name="CommonStates">
                                      <VisualState x:Name="Disabled"/>
                                      <VisualState x:Name="Normal"/>
                                      <VisualState x:Name="MouseOver">
                                                <Storyboard>
                                                          <ColorAnimation Duration="0" To="#FF51E8EB" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle" d:IsOptimized="True"/>
                                                          <ColorAnimation Duration="0" To="#FF0C04FB" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="textBlock" d:IsOptimized="True"/>
                                                </Storyboard>
                                      </VisualState>
                                      <VisualState x:Name="Pressed"/>
                             </VisualStateGroup>
                   </VisualStateManager.VisualStateGroups>
                   <Rectangle x:Name="rectangle" Fill="#FF8E8E97" Stroke="Black"/>
                   <TextBlock x:Name="textBlock" Margin="8,8,19,8" TextWrapping="Wrap" FontWeight="Bold"><Run Text="New TextBox"/><LineBreak/><Run/></TextBlock>
          </Grid>
</
ControlTemplate>

Now we will move to the code behind for the real action.

We are tracking both the MouseRightButtonDown and MouseLeftButtonDown event.

The right click event will look like this:

private void LayoutRoot_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    e.Handled = true;
    sp1.Visibility = Visibility.Visible;
    Canvas.SetLeft(sp1,e.GetPosition(null).X);
    Canvas.SetTop(sp1,e.GetPosition(null).Y);
}


In the first line of code we are forcing to handle the right click event. In the next few lines I am setting the stackpanel visible property to be visible and its position to be the mouse right click position.

The output will be as follows:

In the first two options in the menu, I am adding one textbox and a button to the page. In the third option, I am randomly changing the background of the page.
The code for changing the background color is as follows:

3.gif

private void b3_click(object sender, System.Windows.RoutedEventArgs e)
{
    Random rnd=new Random((int)DateTime.Now.Millisecond);
    SolidColorBrush sb=new SolidColorBrush(Color.FromArgb((byte)rnd.Next(0,255),(byte)rnd.Next(0,255),(byte)rnd.Next(0,255),(byte)rnd.Next(0,255)));
    LayoutRoot.Background=sb;
}

The other two button click events can be viewed from the source code attached with the article.

In the mouse left button click on the page, I am hiding the contextmenu. Here is the code for that:

private void LayoutRoot_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    sp1.Visibility=Visibility.Collapsed;
    LayoutRoot.Children.Remove(t);
    LayoutRoot.Children.Remove(b);
}


That's it.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Author
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.