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>
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;
}
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With