有焦点时更改 wpf 中按钮的背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20949742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change backgorund of button in wpf when it has focus
提问by mohammad
I have a button and I want to change the background color of button. but when I set the background property to a color like blue:
我有一个按钮,我想更改按钮的背景颜色。但是当我将背景属性设置为类似蓝色的颜色时:
<Button Width="75" Height="25" Margin="6" Background="Blue"/>
and when the button has focus, the color changes in white and my color.
当按钮有焦点时,颜色会变成白色和我的颜色。
How can I set this white color to another color?
如何将这种白色设置为另一种颜色?
回答by Sheridan
I believe that your problem is caused by the default ControlTemplatefor the Buttoncontrol. The colour animations that you describe are defined in this ControlTemplateand so you need to provide your own ControlTemplateto remove this behaviour:
我相信,你的问题是由默认引起ControlTemplate的Button控制。您描述的颜色动画在此定义ControlTemplate,因此您需要提供自己的动画ControlTemplate以消除此行为:
<Button Content="Click Me" Background="Blue">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="{TemplateBinding Padding}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsFocused" Value="True">
<Setter TargetName="Border" Property="Background" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
回答by Heena Patil
First Solution is
第一个解决方案是
<Style x:Key="BtnStyle" TargetType="Button">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="gd" Height="120" Width="120" Background="{TemplateBinding Background}">
<ContentPresenter></ContentPresenter>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsFocused" Value="True">
<Setter Property="Background" Value="White" TargetName="gd"></Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Visibility" Value="Collapsed" TargetName="gd"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Button Width="75" Style="{StaticResource BtnStyle }" Content="ok" Height="25" Background="Blue"/>
second solution
第二种解决方案
<Style x:Key="Button_focusvisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border SnapsToDevicePixels="True" Background="White">
<TextBlock Text="Ok" ></TextBlock>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Button Width="75" FocusVisualStyle="{StaticResource Button_focusvisual }" Content="ok" Height="25" Background="Blue"/>

