Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to create contextMenu in UWP App
WhatsApp
Muthuramalingam Duraipandi
Aug 09
2016
2.9
k
0
0
contextMenu.ZIP
// MainPage1.cs
using
System;
using
System.Collections.Generic;
using
Windows.UI.Xaml.Controls;
namespace
SDKTemplate
{
public
partial
class
MainPage : Page
{
public
const
string
FEATURE_NAME =
"Context menu C# sample"
;
List<Scenario> scenarios =
new
List<Scenario>
{
new
Scenario() { Title =
"Show a context menu"
, ClassType =
typeof
(Scenario1) },
new
Scenario() { Title =
"Replace a default context menu"
, ClassType =
typeof
(Scenario2) },
};
}
public
class
Scenario
{
public
string
Title {
get
;
set
; }
public
Type ClassType {
get
;
set
; }
}
}
// Scenario1.cs
using
System;
using
Windows.Foundation;
using
Windows.UI.Popups;
using
Windows.UI.Xaml;
using
Windows.UI.Xaml.Controls;
using
Windows.UI.Xaml.Input;
using
Windows.UI.Xaml.Media;
using
Windows.UI.Xaml.Navigation;
namespace
SDKTemplate
{
public
sealed
partial
class
Scenario1 : Page
{
public
Scenario1()
{
this
.InitializeComponent();
}
public
static
Rect GetElementRect(FrameworkElement element)
{
GeneralTransform buttonTransform = element.TransformToVisual(
null
);
Point point = buttonTransform.TransformPoint(
new
Point());
return
new
Rect(point,
new
Size(element.ActualWidth, element.ActualHeight));
}
private
async
void
AttachmentImage_RightTapped(
object
sender, RightTappedRoutedEventArgs e)
{
var menu =
new
PopupMenu();
menu.Commands.Add(
new
UICommand(
"Open with"
, (command) =>
{
OutputTextBlock.Text =
"'"
+ command.Label +
"' selected"
;
}));
menu.Commands.Add(
new
UICommand(
"Save attachment"
, (command) =>
{
OutputTextBlock.Text =
"'"
+ command.Label +
"' selected"
;
}));
OutputTextBlock.Text =
"Context menu shown"
;
var chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
if
(chosenCommand ==
null
)
// The command is null if no command was invoked.
{
OutputTextBlock.Text =
"Context menu dismissed"
;
}
}
}
}
//scenario2.cs
using
System;
using
Windows.ApplicationModel.DataTransfer;
using
Windows.Foundation;
using
Windows.System;
using
Windows.UI.Popups;
using
Windows.UI.Xaml;
using
Windows.UI.Xaml.Controls;
using
Windows.UI.Xaml.Navigation;
using
Windows.UI.Xaml.Media;
namespace
SDKTemplate
{
public
sealed
partial
class
Scenario2 : Page
{
public
Scenario2()
{
this
.InitializeComponent();
}
private
Rect GetTextboxSelectionRect(TextBox textbox)
{
Rect rectFirst, rectLast;
if
(textbox.SelectionStart == textbox.Text.Length)
{
rectFirst = textbox.GetRectFromCharacterIndex(textbox.SelectionStart - 1,
true
);
}
else
{
rectFirst = textbox.GetRectFromCharacterIndex(textbox.SelectionStart,
false
);
}
int
lastIndex = textbox.SelectionStart + textbox.SelectionLength;
if
(lastIndex == textbox.Text.Length)
{
rectLast = textbox.GetRectFromCharacterIndex(lastIndex - 1,
true
);
}
else
{
rectLast = textbox.GetRectFromCharacterIndex(lastIndex,
false
);
}
GeneralTransform buttonTransform = textbox.TransformToVisual(
null
);
Point point = buttonTransform.TransformPoint(
new
Point());
double
x, y, dx, dy;
y = point.Y + rectFirst.Top;
dy = rectLast.Bottom - rectFirst.Top;
if
(rectLast.Right > rectFirst.Left)
{
x = point.X + rectFirst.Left;
dx = rectLast.Right - rectFirst.Left;
}
else
{
x = point.X + rectLast.Right;
dx = rectFirst.Left - rectLast.Right;
}
return
new
Rect(x, dx, y, dy);
}
private
async
void
ReadOnlyTextBox_ContextMenuOpening(
object
sender, ContextMenuEventArgs e)
{
e.Handled =
true
;
TextBox textbox = (TextBox)sender;
if
(textbox.SelectionLength > 0)
{
var menu =
new
PopupMenu();
menu.Commands.Add(
new
UICommand(
"Copy"
,
null
, 1));
menu.Commands.Add(
new
UICommandSeparator());
menu.Commands.Add(
new
UICommand(
"Highlight"
,
null
, 2));
menu.Commands.Add(
new
UICommand(
"Look up"
,
null
, 3));
OutputTextBlock.Text =
"Context menu shown"
;
Rect rect = GetTextboxSelectionRect(textbox);
var chosenCommand = await menu.ShowForSelectionAsync(rect);
if
(chosenCommand !=
null
)
{
switch
((
int
)chosenCommand.Id)
{
case
1:
String selectedText = ((TextBox)sender).SelectedText;
var dataPackage =
new
DataPackage();
dataPackage.SetText(selectedText);
Clipboard.SetContent(dataPackage);
OutputTextBlock.Text =
"'"
+ chosenCommand.Label +
"'("
+ chosenCommand.Id.ToString() +
") selected; '"
+ selectedText +
"' copied to clipboard"
;
break
;
case
2:
OutputTextBlock.Text =
"'"
+ chosenCommand.Label +
"'("
+ chosenCommand.Id.ToString() +
") selected"
;
break
;
case
3:
OutputTextBlock.Text =
"'"
+ chosenCommand.Label +
"'("
+ chosenCommand.Id.ToString() +
") selected"
;
break
;
}
}
else
{
OutputTextBlock.Text =
"Context menu dismissed"
;
}
}
else
{
OutputTextBlock.Text =
"Context menu not shown because there is no text selected"
;
}
}
}
}
Context
Menu
UWP
windows
Up Next
How to create contextMenu in UWP App