ToggleButton不显示任何状态

时间:2012-09-16 21:08:43

标签: wpf windows-8 togglebutton

我得到了最简单的应用程序:单个窗口,只有一个切换按钮:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ToggleButton Content="This is my ToggleButton" />
    </Grid>
</Window>

当我现在点击切换按钮时,实际上没有任何反应。当我为CheckedUnchecked事件设置事件处理程序,然后单击按钮时,首先Checked然后Unchecked被触发。所以按钮似乎正常工作......

我正在编译.NET 4.5,我正在使用Windows 8 RTM。

此行为是否与Windows 8显示按钮风格相关(否#34; 3D&#34;边框)?任何人都可以确认吗?

更新1 我制作了一张图片来展示我的意思:

Windows 8 vs Windows 7 toggle button

如您所见,在Windows 8&#34中,没有任何反应&#34;当点击切换按钮时,它根本无法切换&#34;切换&#34;。 这似乎是一个错误,与windows 8风格的按钮相关......

更新日期:2013年5月30日: 可以使用修补程序:http://support.microsoft.com/kb/2805222
见WPF下的问题#5 不幸的是,它并没有解决我的问题:(

2 个答案:

答案 0 :(得分:5)

这是WPF中确认的缺陷。解决方法是相应地设置控件的样式,尽管产品组可能会考虑修复。要请求修复,请与Microsoft支持部门联系。

答案 1 :(得分:4)

对于想要开始使用某些代码的每个人,您可以使用我用来设置控件样式的代码:

<Application.Resources>

        <!-- Toogle button fix (includes custom button + toggle button style) -->
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
            <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border BorderThickness="1" BorderBrush="#FFA4A4A4">
                            <Grid>
                                <Rectangle x:Name="Rectangle_Background" Fill="#FFEDEDED" />
                                <ContentPresenter x:Name="ContentPresenter_Content" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter TargetName="Rectangle_Background" Property="Fill" Value="#f7f7f7"/>
                                <Setter TargetName="ContentPresenter_Content" Property="Opacity" Value="0.5"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Rectangle_Background" Property="Fill" Value="#e0e0e0" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
            <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border BorderThickness="1" BorderBrush="#FFA4A4A4">
                            <Grid>
                                <Rectangle x:Name="Rectangle_Background" Fill="#FFEDEDED" />
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter TargetName="Rectangle_Background" Property="Fill" Value="#ADADAD"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Rectangle_Background" Property="Fill" Value="#e0e0e0" />
                            </Trigger>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter Property="Fill" TargetName="Rectangle_Background" Value="#bee6fd"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Application.Resources>