WPF中禁用按钮样式的问题

时间:2018-12-03 07:49:22

标签: wpf xaml controltemplate app.xaml

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

启用按钮后,背景颜色为透明,前景为白色。 再次禁用该按钮时,背景为透明,前景为红色。 谁能帮助我了解我的风格出了什么问题?

1 个答案:

答案 0 :(得分:0)

您已经替换了控件模板,因此现在必须更改作为“主控件”的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>