Its Late but I solved it Some How.i forgot the Blog Link for Code Behing Context menu via Telerik blogs
Custom ContextMenu Class:
public abstract class ContextMenu
{
private Point _location;
private bool _isShowing;
private System.Windows.Controls.Primitives.Popup _popup;
private Grid _grid;
private Canvas _canvas;
private FrameworkElement _content;
//Initialize and display the pop-up form. Public method is called when a menu item is displayed
public void Show(Point location)
{
if (_isShowing)
throw new InvalidOperationException();
_isShowing = true;
_location = location;
ConstructPopup();
_popup.IsOpen = true;
}
//Close pop-up window
public void Close()
{
_isShowing = false;
if (_popup != null)
{
_popup.IsOpen = false;
}
}
//abstract function that the child class needs to implement to return the framework element that needs to be displayed in the popup window.
protected abstract FrameworkElement GetContent();
//Default behavior for OnClickOutside() is to close the context menu when there is a mouse click event outside the context menu
protected virtual void OnClickOutside()
{
Close();
}
// To use Grid layout , initialize the pop form
//In the Grid which added a Canvas, for outside monitoring menu item mouse click events
//
// Add the Framework Element returned by GetContent() to the grid and position it at _location
private void ConstructPopup()
{
if (_popup != null)
return;
_popup = new System.Windows.Controls.Primitives.Popup();
_grid = new Grid();
_popup.Child = _grid;
_canvas = new Canvas();
_canvas.MouseLeftButtonDown += (sender, args) => { OnClickOutside(); };
_canvas.MouseRightButtonDown += (sender, args) => { args.Handled = true; OnClickOutside(); };
_canvas.Background = new SolidColorBrush(Colors.Transparent);
_grid.Children.Add(_canvas);
_content = GetContent();
_content.HorizontalAlignment = HorizontalAlignment.Left;
_content.VerticalAlignment = VerticalAlignment.Top;
_content.Margin = new Thickness(_location.X, _location.Y, 0, 0);
_grid.Children.Add(_content);
UpdateSize();
}
/// <summary>
/// Update Size
/// </summary>
private void UpdateSize()
{
_grid.Width = Application.Current.Host.Content.ActualWidth;
_grid.Height = Application.Current.Host.Content.ActualHeight;
if (_canvas != null)
{
_canvas.Width = _grid.Width;
_canvas.Height = _grid.Height;
}
}
}
ContextMenuItem Class
public class ContextMenuItem : ViewModelBase
{
public ContextMenuItem()
{
this.SubItems = new ObservableCollection<ContextMenuItem>();
}
private Decimal _Id;
public Decimal Id
{
get
{
return _Id;
}
set
{
_Id = value;
RaisePropertyChanged(() => Id);
}
}
private string _Text;
public string Text
{
get
{
return _Text;
}
set
{
_Text = value;
RaisePropertyChanged(() => Text);
}
}
private Uri _IconUrl;
public Uri IconUrl
{
get
{
return _IconUrl;
}
set
{
_IconUrl = value;
RaisePropertyChanged(() => IconUrl);
}
}
private bool _IsSeparator;
public bool IsSeparator
{
get
{
return _IsSeparator;
}
set
{
_IsSeparator = value;
RaisePropertyChanged(() => IsSeparator);
}
}
private ICommand _Command;
public ICommand Command
{
get
{
return _Command;
}
set
{
_Command = value;
RaisePropertyChanged(() => Command);
}
}
private ObservableCollection<ContextMenuItem> _SubItems;
public ObservableCollection<ContextMenuItem> SubItems
{
get
{
return _SubItems;
}
set
{
_SubItems = value;
RaisePropertyChanged(() => SubItems);
}
}
private object _CommandParameter;
public object CommandParameter
{
get
{
return _CommandParameter;
}
set
{
_CommandParameter = value;
RaisePropertyChanged(() => CommandParameter);
}
}
}
RightContextMapMenu Class
public class RightContextMapMenu : ContextMenu
{
public RightContextMapMenu()
{
}
public RightContextMapMenu(ObservableCollection<ContextMenuItem> MapContextMenuItems)
{
this.MapContextMenuItems = MapContextMenuItems;
}
protected FrameworkElement GetContentRad()
{
return null;
}
private RadContextMenu BuildContextMenu()
{
RadContextMenu contextMenu = new RadContextMenu();
contextMenu.ItemContainerStyle = Application.Current.Resources["HpmMapItemsMenuItemContainer"] as Style;
contextMenu.ItemsSource = InitializeRowContextMenuItems();
return contextMenu;
}
private ObservableCollection<ContextMenuItem> InitializeRowContextMenuItems()
{
if (MapContextMenuItems == null)
{
ObservableCollection<ContextMenuItem> items = new ObservableCollection<ContextMenuItem>();
ContextMenuItem gmapMenuItem = new ContextMenuItem();
gmapMenuItem.Id = 1;
gmapMenuItem.Text = "Google Map ";
gmapMenuItem.Command = new DelegateCommand(OnGoogleMapOpen, CanGoogleMapOpen);
items.Add(gmapMenuItem);
//for (var i = 0; i < 5; i++)
//{
// ContextMenuItem ctx = new ContextMenuItem();
// ctx.Id = i;
// ctx.Text = "Hola" + i;
// ctx.Command = new DelegateCommand(OnContextMenuItemCommand, CanContextMenuItemCommand);
// ctx.CommandParameter = i;
// items.Add(ctx);
//}
return items;
}
else
{
return MapContextMenuItems;
}
}
public ObservableCollection<ContextMenuItem> MapContextMenuItems { get; set; }
private void OnGoogleMapOpen(object obj)
{
}
private bool CanGoogleMapOpen(object obj)
{
return true;
}
private bool CanContextMenuItemCommand(object obj)
{
return true;
}
private void OnContextMenuItemCommand(object obj)
{
}
// Construct a menu button and return to a FrameworkElement object
protected override FrameworkElement GetContent()
{
return BuildContextMenu();
}
}
Style Code:
<!-- RIGHT CLICK MENU-->
<Style x:Key="HpmMapItemsMenuItemContainer" TargetType="telerik:RadMenuItem">
<Setter Property="Icon" Value="{Binding IconUrl}"/>
<Setter Property="IconTemplate">
<Setter.Value>
<DataTemplate>
<Image Source="{Binding}" Stretch="None"/>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="IsSeparator" Value="{Binding IsSeparator}"/>
<Setter Property="Header" Value="{Binding Text}"/>
<Setter Property="ItemsSource" Value="{Binding SubItems}"/>
<Setter Property="Command" Value="{Binding Command}"/>
<Setter Property="CommandParameter" Value="{Binding CommandParameter}"/>
</Style>
At Map Right Click Event Command Bing with MVVMLight
public MainMap(){
MapRightContextMenu = CreateMapRightMenu();
this.MouseRightButtonDownCommand = new GalaSoft.MvvmLight.Command.RelayCommand<object>(OnMouseRightButtonDownCommand, CanMouseRightButtonDownCommand);
}
public RightContextMapMenu CreateMapRightMenu()
{
RightContextMapMenu menu = new RightContextMapMenu();
menu.MapContextMenuItems = createRightClickMapMenu();
return menu;
}
private RightContextMapMenu _MapRightContextMenu;
public RightContextMapMenu MapRightContextMenu
{
get
{
return _MapRightContextMenu;
}
set
{
_MapRightContextMenu = value;
RaisePropertyChanged(() => MapRightContextMenu);
}
}
/// <returns></returns>
private bool CanMouseRightButtonDownCommand(object arg)
{
return true;
}
/// <summary>
/// At Right Click
/// </summary>
/// <param name="obj"></param>
private void OnMouseRightButtonDownCommand(object obj)
{
MouseButtonEventArgs args = obj as MouseButtonEventArgs;
Point mapPoint=args.GetPosition(BaseVm.HpmMap.Parent as UIElement);
args.Handled = true;
if (MapRightContextMenu != null)
{
MapRightContextMenu.Show(mapPoint);
}
}
private ObservableCollection<ContextMenuItem> createRightClickMapMenu()
{
ObservableCollection<ContextMenuItem> items = new ObservableCollection<ContextMenuItem>();
ContextMenuItem gmapMenuItem = new ContextMenuItem();
gmapMenuItem.Id = 1;
gmapMenuItem.Text = "Google Map ";
items.Add(gmapMenuItem);
ContextMenuItem gmapMenuItem1 = new ContextMenuItem();
gmapMenuItem.Id = 2;
gmapMenuItem.Text = "Youtube Map ";
items.Add(gmapMenuItem1);
return items;
}
Its working for me. But I have an issue when Postion. I dont know how to resolve.