MenuItem IsCheckable属性。 UI不表示菜单项是可检查的

时间:2013-06-19 21:23:13

标签: c# wpf visual-studio-2010

认为当我之前使用IsCheckable属性时,UI显示了复选框。

现在没有迹象表明菜单项是可检查的。 这是正确的行为吗?

(当我点击菜单项时,复选标记会切换。)

enter image description here enter image description here

<MenuItem Header="Tools" Name="miTools">
    <MenuItem Header="Log data communication">
        <MenuItem Header="Log complete data packets" Name="miLogFullPacket" IsCheckable="True" IsChecked="False" />
        <MenuItem Header="Log fragments of data packet" Name="miLogPacketFragment" IsCheckable="True" IsChecked="False" Click="logDataPacketFragments_Click"/>
        <MenuItem Header="Show log file"  />
    </MenuItem>
    <MenuItem Header="Settings..." Click="settingsChange_Click"/>
</MenuItem>

1 个答案:

答案 0 :(得分:1)

这是因为默认菜单的 Style 具有以下代码:

<!-- CheckMark in resources -->
<PathGeometry x:Key="Checkmark" Figures="M0,2 L0,4.8 L2.5,7.4 L7.1,2.8 L7.1,0 L2.5,4.6 z" />

<!-- Template for SubmenuItem --> 
<ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition MinWidth="17" SharedSizeGroup="MenuItemIconColumnGroup" Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto"/>
                <ColumnDefinition Width="14"/>
            </Grid.ColumnDefinitions>

            <!-- Place for Icon --> 
            <ContentPresenter x:Name="Icon" ContentSource="Icon" Margin="4,0,6,0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>

            <!-- CheckMark path: initially hidden -->
            <Path x:Name="GlyphPanel" Data="{StaticResource Checkmark}" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="4,0,6,0" Visibility="Hidden" VerticalAlignment="Center"/>

            <ContentPresenter Grid.Column="1" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>

            <!-- MenuItem Text -->
            <TextBlock x:Name="InputGestureText" Grid.Column="2" DockPanel.Dock="Right" Margin="5,2,0,2" Text="{TemplateBinding InputGestureText}"/>
        </Grid>
    </Border>

    <ControlTemplate.Triggers>

    <!-- ... -->

        <!-- This show CheckMark -->
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
        </Trigger>

    <!-- ... -->
    </ControlTemplate.Triggers>
</ControlTemplate>

在触发器中,此条件仅被检查(IsChecked == True),但没有动作,if(IsCheckable == True)。 MenuItem Style MSDN 示例包含以下代码:

<!-- ... -->

<Border Name="Check" Width="13" Height="13" Visibility="Collapsed" Margin="6,0,6,0" Background="{StaticResource NormalBrush}" BorderThickness="1" BorderBrush="{StaticResource NormalBorderBrush}">
    <Path Name="CheckMark" Width="7" Height="7" Visibility="Hidden" SnapsToDevicePixels="False" Stroke="{StaticResource GlyphBrush}" StrokeThickness="2" Data="M 0 0 L 7 7 M 0 7 L 7 0" />
</Border>

<ContentPresenter Name="HeaderHost" Grid.Column="1" ContentSource="Header" RecognizesAccessKey="True"/>

<!-- MenuItem Text -->
<TextBlock x:Name="InputGestureText" Grid.Column="2" Text="{TemplateBinding InputGestureText}" Margin="5,2,0,2" DockPanel.Dock="Right" />

<!-- ... -->

<ControlTemplate.Triggers>               

    <!-- ... -->

    <Trigger Property="IsChecked" Value="true">
        <Setter TargetName="CheckMark" Property="Visibility" Value="Visible"/>
    </Trigger>

    <Trigger Property="IsCheckable" Value="true">
        <Setter TargetName="Check" Property="Visibility" Value="Visible"/>
        <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
    </Trigger>

    <!-- ... -->

</ControlTemplate.Triggers>

如果MenuItem(IsCheckable == true)它显示Border&#34; Check&#34;空内容(没有CheckMark)。

输出:使用 MSDN 样式并根据需要进行更改。