【问题标题】:Problem with disabled button style in WPFWPF中禁用按钮样式的问题
【发布时间】:2018-12-03 07:49:21
【问题描述】:

我在 App.xaml 文件中使用波纹管样式。我希望我的按钮(SendCommandBtn)在启用时具有 #5e95a7 颜色,并且没有圆角边框和其他一些样式。

 <Style TargetType="{x:Type Button}" x:Key="SendCommandBtn">
        <Setter Property="Height" Value="28"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="#5e95a7"/>
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate  TargetType="{x:Type Button}" >
                    <Border CornerRadius="0"  BorderThickness="0"  Height="28" Margin="0,0,10,0" >
                        <ContentPresenter VerticalAlignment="Center"  HorizontalAlignment="Center"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" Value="#f2f2f2"/>
                            <Setter Property="Foreground" Value="Red"/>
                            <Setter Property="Cursor" Value="No"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="True">
                            <Setter Property="Background" Value="#5e95a7"/>
                            <Setter Property="Foreground" Value="#fff"/>
                            <Setter Property="Cursor" Value="Hand"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>

    <Style TargetType="{x:Type Button}"  x:Key="NormalBtn">
        <Setter Property="Background" Value="#5e95a7"/>
        <Setter Property="Height" Value="28"/>
        <Setter Property="Foreground" Value="#fff"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="#5e95a7"/>
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate  TargetType="{x:Type Button}" >
                    <Border CornerRadius="0"  BorderThickness="0" Background="#5e95a7" Height="28" Margin="0,0,10,0" >
                        <ContentPresenter VerticalAlignment="Center"  HorizontalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

现在当我有下面的按钮时:

 <Button Name="BtnSIM2" Content=" SIM 2" Grid.Column="2" Click="BtnSIM2_Click" Style="{DynamicResource SendCommandBtn}"></Button>

当按钮启用时,背景颜色为透明,前景为白色。 当按钮再次禁用时,背景是透明的,前景是红色的。 谁能帮我理解我的风格有什么问题?

【问题讨论】:

    标签: wpf xaml controltemplate app.xaml


    【解决方案1】:

    您已经替换了控件模板,因此您现在必须更改作为“主控件”的 Border 的背景。 为此,请在内部设置边框的名称并更改该边框的颜色,而不是像这样(通过以其名称定位该边框):

    <Style TargetType="{x:Type Button}" x:Key="SendCommandBtn">
       ...
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate  TargetType="{x:Type Button}" >
                    <Border CornerRadius="0"  BorderThickness="0"  Height="28" Margin="0,0,10,0" x:Name="ButtonBorder" >
                        <ContentPresenter VerticalAlignment="Center"  HorizontalAlignment="Center"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" TargetName="ButtonBorder" Value="#f2f2f2"/>
                            <Setter Property="Foreground" TargetName="ButtonBorder" Value="Red"/>
                            <Setter Property="Cursor" TargetName="ButtonBorder" Value="No"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" TargetName="ButtonBorder" Value="True">
                            <Setter Property="Background" TargetName="ButtonBorder" Value="#5e95a7"/>
                            <Setter Property="Foreground" TargetName="ButtonBorder" Value="#fff"/>
                            <Setter Property="Cursor" TargetName="ButtonBorder" Value="Hand"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    
    </Style>
    

    【讨论】:

    • 谢谢你亲爱的 Martin Ch
    猜你喜欢
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多