【问题标题】:ToggleButton doesn't show any stateToggleButton 不显示任何状态
【发布时间】:2012-09-09 04:00:38
【问题描述】:

我有有史以来最简单的应用程序:带有一个切换按钮的单个窗口:

<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
不幸的是,它并没有解决我的问题 :(

【问题讨论】:

  • “什么都没有发生”到底是什么意思?单击按钮时是否未按下按钮?如果是这种情况,那么它可能与您所说的 Windows 8 有关。
  • 是的...什么也没发生是什么意思?按钮不掉吗?
  • 也许你应该为切换按钮定义一个大小
  • 我遇到了同样的问题。非常烦人...我为您的帖子在 Microsoft Connect 上投票
  • 今天这个退出有什么解决办法吗?我有同样的问题(不仅是 ToggleButton),但普通按钮在 Windows 8 上的 WPF 应用程序中看起来很糟糕。

标签: wpf windows-8 togglebutton


【解决方案1】:

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

【讨论】:

【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2016-02-11
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 2019-05-27
    相关资源
    最近更新 更多