在 WPF 中设置按钮的选定颜色和焦点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13476487/
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
Setting a button's selected color and focus in WPF
提问by Seb
I have a button that has a transparent background and a thick white border. When the button detects a mouse enter I see that the background went from the set transparent background to it's default focused color so for that I just set the opacity to 0.2 so that it shows some feed back that it has a focus.
我有一个按钮,它有一个透明的背景和一个厚厚的白色边框。当按钮检测到鼠标进入时,我看到背景从设置的透明背景变为默认聚焦颜色,因此我将不透明度设置为 0.2,以便它显示一些反馈,表明它具有焦点。
Now here is my dilemma. When I leave the button's bounds there is a small animation that goes from my .2 opacity to 1 before changing the background to the original transparent. I would like to know how to replace this with either a smoother animation so that I don't see the opaque background before it changing to transparent or just have a way to bypass the animation completely and just have it set my values. I see the similar thing happen when the button is focused. It will animate between 0.2 opacity and 1.0 with the default background color. Any ideas would be greatly appreciated.
现在这是我的困境。当我离开按钮的边界时,在将背景更改为原始透明之前,有一个小动画从我的 .2 不透明度变为 1。我想知道如何用更平滑的动画替换它,以便在它变为透明之前我看不到不透明的背景,或者只是有一种方法可以完全绕过动画并让它设置我的值。当按钮聚焦时,我看到类似的事情发生。它将使用默认背景颜色在 0.2 不透明度和 1.0 之间设置动画。任何想法将不胜感激。
<Button BorderBrush="White" BorderThickness="1" Width="45" Height="45" >
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="0.2" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Opacity" Value="1.0" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
回答by Blachshma
Here is a XAML only solution, I think this does what you want...
这是一个仅 XAML 的解决方案,我认为这可以满足您的需求...
<Button BorderBrush="White" BorderThickness="1" Width="45" Height="45" Content="1234" >
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Opacity" Value="1.0" />
</Trigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.2" To="1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.2" To="0.2"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>

