wpf奇怪的MenuItem可见性问题

时间:2009-12-10 14:02:01

标签: wpf visibility menuitem

我有一个简单的场景,我隐藏并在MenuItem中显示一个#{1}},如下所示:

Xaml:

ContextMenu
代码背后的代码:

<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次点击确定,依此类推......

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

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

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

例如:

If ContextMenu Is Nothing Then
    ContextMenu = mainMnu
Else
    ContextMenu = Nothing
End If