1

我有一个简单的场景,我在 a 中隐藏和显示一个信号MenuItemContextMenu如下所示:

xml:

<Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Window.ContextMenu>
        <ContextMenu Name="mainMnu">
            <MenuItem Name="mnu" Header="Testing"/>
        </ContextMenu>
    </Window.ContextMenu>

    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Button HorizontalAlignment="Left" Margin="52,131,0,108" Name="Button1"
                Width="75">Button</Button>
    </Grid>
</Window>

后面的代码:

Private Sub Button1_Click(ByVal sender As System.Object,
                          ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    If mnu.Visibility = Windows.Visibility.Visible Then
        mnu.Visibility = Windows.Visibility.Collapsed
        mainMnu.Visibility = Windows.Visibility.Collapsed
    Else
        mnu.Visibility = Windows.Visibility.Visible
        mainMnu.Visibility = Windows.Visibility.Visible
    End If

End Sub

问题是每隔一次我按Button没有MenuItem显示。右键单击仅显示一个小的空矩形。(就像有一个可见的菜单,但不是一个可见的MenuItem)。它以某种方式循环工作......前2次点击:问题,接下来2次点击ok,等等......

有任何想法吗?

4

1 回答 1

3

如果您尝试禁用上下文菜单,则设置它Visibility是错误的方法。

相反,您应该将该ContextMenu属性设置为Nothing.

例如:

If ContextMenu Is Nothing Then
    ContextMenu = mainMnu
Else
    ContextMenu = Nothing
End If
于 2009-12-10T14:05:55.980 回答