Silverlight Context Menu Simple Example:
Silverlight Context Menu Simple Example:
Here I will explain how to create simple context menu in silverlight. The output screen will be looks like below.
Design Page:
In design page I just use one treeview and two grids(We add context menus in this grids).
xaml CODE:
Add the following code inside UserControls.<Grid x:Name="LayoutRoot" Background="White">
<sdk:TreeView Height="347" HorizontalAlignment="Left" Margin="30,0,0,0" Name="treeView1" VerticalAlignment="Top" Width="265" />
<Grid Height="auto" HorizontalAlignment="Left" Margin="368,230,0,0" Name="grid1" VerticalAlignment="Top" Width="auto" />
<Grid Height="auto" HorizontalAlignment="Left" Margin="368,230,0,0" Name="grid2" VerticalAlignment="Top" Width="auto" />
</Grid>
C# CODE:
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.IO;
using System.Windows.Media.Imaging;
namespace Sample
{
public partial class MainPage : UserControl
{
{
{
//Create context menu here=============================
public MainPage()
{
TreeViewItem tv = sender as TreeViewItem;
string itemname = tv.Header.ToString();
{
InitializeComponent();
//context menu 1
ContextMenu cm = new ContextMenu();
MenuItem copy = new MenuItem();
copy.Header = "Copy";
copy.Click += new RoutedEventHandler(copy_Click);
cm.Items.Add(copy);
grid1.Children.Add(cm);
grid1.Visibility = System.Windows.Visibility.Collapsed;
//context menu 2
ContextMenu cm2 = new ContextMenu();
MenuItem copychild = new MenuItem();
copychild.Header = "CopyChild";
Image addicon = new Image(); //if we want add icons in context menu the use this code
addicon.Source = new BitmapImage(new Uri("Icons/copyicon.jpeg", UriKind.Relative));
addicon.Width = 16;
addicon.Height = 16;
copychild.Icon = addicon;
copychild.Click += new RoutedEventHandler(copychild_Click);
cm2.Items.Add(copychild);
grid2.Children.Add(cm2);
grid2.Visibility = System.Windows.Visibility.Collapsed;
addtreeview();
}
//adding data to treeview==================================================
private void addtreeview()
{
TreeViewItem tv = new TreeViewItem();
tv.Header = "title";
tv.MouseRightButtonDown += new MouseButtonEventHandler(tv_MouseRightButtonDown);
treeView1.Items.Add(tv);
for (int i = 0; i < 5; i++)
{
TreeViewItem tvc = new TreeViewItem();
tvc.Header = "Child " + i;
tvc.MouseRightButtonDown += new MouseButtonEventHandler(tvc_MouseRightButtonDown);
tv.Items.Add(tvc);
}
}
//treeview Right click events==============================================
void tv_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
double a = e.GetPosition(LayoutRoot).X;
double b = e.GetPosition(LayoutRoot).Y;
grid1.Margin = new Thickness(a, b, 0, 0);
grid1.Visibility = System.Windows.Visibility.Visible;
}
void tvc_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
double a = e.GetPosition(LayoutRoot).X;
double b = e.GetPosition(LayoutRoot).Y;
grid2.Margin = new Thickness(a, b, 0, 0);
grid2.Visibility = System.Windows.Visibility.Visible;
//If you want to get the item name of which you are clicking then use this codeTreeViewItem tv = sender as TreeViewItem;
string itemname = tv.Header.ToString();
}
//context menu click events================================================
private void copy_Click(object sender, RoutedEventArgs e)
{
grid1.Visibility= System.Windows.Visibility.Collapsed;
grid2.Visibility= System.Windows.Visibility.Collapsed;
MessageBox.Show("copied");
}
void copychild_Click(object sender, RoutedEventArgs e)
{
grid1.Visibility= System.Windows.Visibility.Collapsed;
grid2.Visibility= System.Windows.Visibility.Collapsed;
MessageBox.Show("copied Child");
}
}
}
Comments
Post a Comment