[英]Change button color when disabled
I use the following button and when isDisabled is true the button is transparent (here its always true for the example but in my program its bound to command...) what I want is that when the button is disabled the color remain the same just ligther, how can I achive that? 我使用以下按钮,当isDisabled为true时,该按钮是透明的(此处示例始终为true,但在我的程序中它与command绑定...)我想要的是,当禁用按钮时,颜色保持不变ligther,我该如何实现?
<Button
Width="100"
Height="25"
Background="#f0ab00"
Content="Run"
FontSize="16"
Foreground="#ffffff"
IsDefault="True"
Margin="10,0,20,0"
IsEnabled="false" />
</Grid>
You have to edit or make a ControlTemplate for the Button. 您必须编辑或制作Button的ControlTemplate。 (use a key to bind the controltemplate to the button).
(使用键将控件模板绑定到按钮)。
<Window.Resources>
<ControlTemplate x:Key="ButtonBaseControlTemplate1" TargetType="{x:Type ButtonBase}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsDefaulted" Value="True">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="border" Value="#FFBEE6FD"/>
<Setter Property="BorderBrush" TargetName="border" Value="#FF3C7FB1"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="border" Value="#FFC4E5F6"/>
<Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Setter Property="Background" TargetName="border" Value="#FFBCDDEE"/>
<Setter Property="BorderBrush" TargetName="border" Value="#FF245A83"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" TargetName="border" Value="Red"/>
<Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
Control: 控制:
<Button
Width="100"
Height="25"
Background="#f0ab00"
Content="Run"
FontSize="16"
Foreground="#ffffff"
IsDefault="True"
Margin="10,0,20,0"
IsEnabled="false" Template="{DynamicResource ButtonBaseControlTemplate1}" />
<Style TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="#ffffff"/>
<Setter Property="Background" Value="#f0ab00"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" Value="#f0aa00"/>
</Trigger>
</Style.Triggers>
</Style>
You can play with the colors . 您可以玩这些颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.