7 Answers, 1 is accepted
Yes, it is possible to use ContextMenu on all kind of controls. Please note that in order to have context menu on right click you have to set your Silverlight application to be windowless(this is done in your .aspx file) .
I have attached a simple example how it is done. Please let me know if you need more help.
Kind regards,
Boyan
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Is it possible to do this application-wide so all textboxes have the right-click copy, cut, and paste functionality rather than implement this for each textbox in my application?
Please let me know.
Thanks
Using single instance of RadContextMenu on multiple controls is not supported. So you cannot do application wide context menus.
We will try to improve this in some future release.
Sincerely yours,
Hristo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
RadContextMenu have UIElement property that you can use to get the element on which it is attached.
Note that if you use single instance of RadContextMenu when attach it to new control it will detach from the old one (e.g. cannot use single instance on multiple controls).
All the best,
Hristo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Thanks, that was what I needed. I wanted to have several textboxes with context menus for cut, copy, and paste, but only wanted one set of functions to handle them all. In case it helps anyone else below is my code(modified from a previous telerik example):
The xaml copied to each textbox:
<telerikNavigation:RadContextMenu.ContextMenu>
<telerikNavigation:RadContextMenu Opened="ContextMenuOpened" Closed="ContextMenuClosed" >
<telerikNavigation:RadMenuItem Header="Cut" Click="RadMenuItem_Click" />
<telerikNavigation:RadMenuItem Header="Copy" Click="RadMenuItem_Click" />
<telerikNavigation:RadMenuItem Header="Paste" Click="RadMenuItem_Click" />
</telerikNavigation:RadContextMenu>
</telerikNavigation:RadContextMenu.ContextMenu>
The generic event handling functions (I am using the extension method from the telerik post with the download - ContextMenuOnTextBox_6E924F29):
private void RadMenuItem_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
Telerik.Windows.Controls.
RadMenuItem item = e.OriginalSource as Telerik.Windows.Controls.RadMenuItem;
Telerik.Windows.Controls.
RadContextMenu menu = item.Parent as Telerik.Windows.Controls.RadContextMenu;
if (menu != null)
{
TextBox txt = menu.UIElement as TextBox;
if (txt != null)
{
switch (item.Header.ToString())
{
case "Cut":
txt.Cut();
break;
case "Copy":
txt.Copy();
break;
case "Paste":
txt.Paste();
break;
}
}
}
}
private void ContextMenuOpened(object sender, RoutedEventArgs e)
{
// Customize the context menu depending on the selection and the clipboard content
Telerik.Windows.Controls.
RadContextMenu menu = sender as Telerik.Windows.Controls.RadContextMenu;
if (menu != null)
{
TextBox txt = menu.UIElement as TextBox;
if (txt != null)
{
bool hasSelection = txt.SelectionLength > 0;
(menu.Items[
0] as Telerik.Windows.Controls.RadMenuItem).IsEnabled = hasSelection; // Cut
(menu.Items[
1] as Telerik.Windows.Controls.RadMenuItem).IsEnabled = hasSelection; // Copy
bool hasClipboardContent = !string.IsNullOrEmpty(Clipboard.Paste());
(menu.Items[
2] as Telerik.Windows.Controls.RadMenuItem).IsEnabled = hasClipboardContent; // Paste
}
}
}
private void ContextMenuClosed(object sender, System.Windows.RoutedEventArgs e)
{
Telerik.Windows.Controls.
RadContextMenu menu = sender as Telerik.Windows.Controls.RadContextMenu;
if (menu != null)
{
TextBox txt = menu.UIElement as TextBox;
if (txt != null)
{
// RadMenu and RadContextMenu steal the focus in order to provide
// keyboard navigation. That's why we need to focus the text box
// again when the menu is closed.
txt.Focus();
}
}
}
Just some small tips. RadContextMenu and RadMenu expose ItemClick event so you can use it instead of adding handles to all RadMenuItems.
Or even better approach is to use RoutedCommands instead of events.
All the best,
Hristo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.