如何在左键单击而不是右键单击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>
答案 0 :(得分:7)
试试这个:
// 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;
}
答案 1 :(得分:1)
当您在一个控件上有多个带有上下文菜单的按钮时,单击事件会导致出现问题,并显示上下文菜单的位置。
当您单击button1,菜单将打开,将鼠标移动到button2,单击一次,菜单将关闭并单击另一个而不移动鼠标时,会出现这种情况。新的菜单打开位置,其中是button1的菜单。
要解决此问题,您需要重建Silverlight Toolkit项目。
像这样:内部DependencyObject所有者
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);
}
}
}
}
构建它,在Silverlight项目中更改对新dll的引用并在按钮中设置
ClickMode =&#34;悬停&#34;
<Button Content="Contextmenu" x:Name="button1" ClickMode="Hover" >
<toolkit:ContextMenuService.ContextMenu >
<toolkit:ContextMenu Name="contextMenu1" >
<toolkit:MenuItem />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</Button>