WPF 中禁用按钮样式的问题
Problem with disabled button style in WPF
我在 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>
启用按钮后,背景颜色为透明,前景为白色。
当按钮再次禁用时,背景是透明的,前景是红色的。
任何人都可以帮助我了解我的风格有什么问题吗?
您已经更换了控件模板,因此您现在必须更改 "main control" 边框的背景。
为此,请在内部设置边框的名称并更改该边框的颜色,而不是像这样(通过名称定位该边框):
<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>
我在 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>
启用按钮后,背景颜色为透明,前景为白色。 当按钮再次禁用时,背景是透明的,前景是红色的。 任何人都可以帮助我了解我的风格有什么问题吗?
您已经更换了控件模板,因此您现在必须更改 "main control" 边框的背景。 为此,请在内部设置边框的名称并更改该边框的颜色,而不是像这样(通过名称定位该边框):
<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>