ToggleButton没有显示任何状态。

9

我有一个非常简单的应用程序:一个窗口和一个切换按钮:

<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显示按钮的风格有关吗(没有“3D”边框)?有人可以确认吗?
更新1 我制作了一张图片来展示我的意思:
如你所见,在Windows 8中,当单击切换按钮时,“什么也不会发生”,它根本没有被“切换”。 这似乎是一个错误,与Windows 8显示按钮的风格有关...
更新:2013年5月30日: 有一个热修复程序可用:http://support.microsoft.com/kb/2805222 请参见WPF下的问题#5。 不幸的是,对我来说它并没有修复问题 :(

1
你所说的“什么都没发生”是指什么?当你点击按钮时,它是否没有出现按下的状态?如果是这种情况,那么可能与你所说的Windows 8有关。 - Tejas Sharma
是的...你说的“什么都没发生”是指什么?按钮没有按下吗? - alansiqueira27
也许你应该为切换按钮定义一个大小。 - alansiqueira27
我也遇到了同样的问题。非常烦人... 我在Microsoft Connect上为你的帖子投了票。 - Andrey Morozov
今天有没有解决这个退出的问题?我也遇到了同样的问题(不仅是ToggleButton),但在我的WPF应用程序中,普通按钮在Windows 8上看起来很糟糕。 - sam1589914
2个回答

5

这是WPF中已确认的缺陷。解决方法是相应地设置控件的样式,尽管产品组可能会考虑修复。如需修复,请联系微软支持。


由于没有人回答我的问题,我已经一段时间前翻译了这篇关于编程的内容。链接在这里:https://connect.microsoft.com/VisualStudio/feedback/details/763597/togglebutton-doesnt-show-any-state - GameScripting

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接