1

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

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

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

在此处输入图像描述 在此处输入图像描述

<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>
4

1 回答 1

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),但如果 (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),它会显示带有空内容的边框“检查”(没有 CheckMark)。

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

于 2013-06-20T05:40:23.127 回答