【问题标题】:ToggleButton Style; Override IsEnabled切换按钮样式;覆盖 IsEnabled
【发布时间】:2015-09-01 18:04:31
【问题描述】:

选中 ToggleButton 后,应将 Button 设置为禁用并且前景色应更改。以下方法不起作用:

      <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="Hallo" Margin="113,97,72,46" >
        <ToggleButton.Style>
            <Style TargetType="{x:Type ToggleButton}">
                <Setter Property="Foreground" Value="Blue"/>
                <Setter Property="IsEnabled" Value="True"/>
                <Setter Property="IsChecked" Value="False"/>
                <Style.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="IsEnabled" Value="False"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="Green"/>
                    </Trigger>

                </Style.Triggers>
            </Style>
        </ToggleButton.Style>
    </ToggleButton>

</Grid>

我该怎么做?

【问题讨论】:

  • 我在 Visual Studio 中粘贴了你的代码,它工作了
  • 前景色变成绿色了吗?
  • 不可能,文本颜色默认为灰色。你能想象为什么吗?
  • 发布所有的 xaml,你在使用后面的代码吗?
  • 我不知道,你是道尔顿吗?

标签: wpf


【解决方案1】:

不同版本的 Windows 和不同的 Windows 帐户设置等,禁用控件的外观不同,这就是为什么它适用于其他人而不适用于你。一般来说,你应该尊重这样的设置,但如果你真的很固执,那么你需要明确地覆盖模板,如this question所示:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
            <Grid Background="{TemplateBinding Background}">
                <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}"
                        HorizontalAlignment="Center" 
                        VerticalAlignment="Center" />
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    • 1970-01-01
    相关资源
    最近更新 更多