Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open context menu on left click instead of right click in silverlight

How to open context menu on left click instead of right click in silverlight

<Button Content="Add" Command="{Binding AddTemplateCommand}" Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="14,18,0,0" Name="button1" VerticalAlignment="Top" Width="75" >
    <toolkit:ContextMenuService.ContextMenu >
        <toolkit:ContextMenu Name="contextMenu1" >
            <toolkit:MenuItem Name="FixedToken" Header="FixedToken" ></toolkit:MenuItem>
            <toolkit:MenuItem Name="SequenceToken" Header="SequenceTokenToken" ></toolkit:MenuItem>                             
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>
</Button>
like image 659
Hadi zamani Avatar asked Feb 22 '23 14:02

Hadi zamani


2 Answers

Try this:

// On click
private void button1_Click(object sender, RoutedEventArgs e)
{
    ContextMenuService.GetContextMenu(button1).IsOpen = true;
}

// On right-click
private void button1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
}
like image 58
Mehdi Golchin Avatar answered May 02 '23 13:05

Mehdi Golchin


When You have more than one button with context menu on one control, the click event causes problems with position of context menu.

This situation occurs when you click on button1, the menu will be open, move mouse to button2, click once, the menu will be closed and click another one without moving mouse. New menu opens on postion where was menu from button1.

To resolve this problem, You need to rebuild Silverlight Toolkit project.

  1. Download source code from https://silverlight.codeplex.com
  2. Open project Control.Input.Toolkit -> ContextMenu -> ContextMenu.cs and change

internal DependencyObject Owner

like this:

internal DependencyObject Owner
        {
            get { return _owner; }
            set
            {
                if (null != _owner)
                {
                    FrameworkElement ownerFrameworkElement = _owner as FrameworkElement;
                    if (null != ownerFrameworkElement)
                    {
                        ownerFrameworkElement.MouseLeftButtonDown -= new MouseButtonEventHandler(HandleOwnerMouseRightButtonDown);
                        ownerFrameworkElement.MouseRightButtonDown -= new MouseButtonEventHandler(HandleOwnerMouseRightButtonDown);
                    }
                }
                _owner = value;
                if (null != _owner)
                {
                    FrameworkElement ownerFrameworkElement = _owner as FrameworkElement;
                    if (null != ownerFrameworkElement)
                    {
                        ownerFrameworkElement.MouseLeftButtonDown += new MouseButtonEventHandler(HandleOwnerMouseRightButtonDown);
                        ownerFrameworkElement.MouseRightButtonDown += new MouseButtonEventHandler(HandleOwnerMouseRightButtonDown);
                    }
                }
            }
        }

Build it, change references to new dll's in your Silverlight project and set in button

ClickMode="Hover"

<Button Content="Contextmenu" x:Name="button1" ClickMode="Hover" >
    <toolkit:ContextMenuService.ContextMenu >
        <toolkit:ContextMenu Name="contextMenu1" >
            <toolkit:MenuItem />                         
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>
</Button>
like image 25
Jacek Gzel Avatar answered May 02 '23 12:05

Jacek Gzel