C# WPF:更改鼠标左键下边框的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2208447/
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
WPF: Change background color of border on left mouse button down
提问by Deniz Dogan
Below is a style I use for buttons in my application. Now I'm trying to change the background color of the Border
element that has the name "Background"
when the user clicks the button with the left mouse button.
下面是我在应用程序中用于按钮的样式。现在Border
,"Background"
当用户用鼠标左键单击按钮时,我正在尝试更改具有名称的元素的背景颜色。
How do I do that?
我怎么做?
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="#6e6964" BorderThickness="1" CornerRadius="1" Margin="{TemplateBinding Margin}" SnapsToDevicePixels="True">
<Border BorderBrush="White" BorderThickness="1" CornerRadius="1" SnapsToDevicePixels="True">
<Border Padding="12,4,12,4" SnapsToDevicePixels="True" Name="Background">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="#f1f1f1" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="#edf8fb"/>
<GradientStop Offset="1" Color="#e2edf0"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
采纳答案by Rob Fonseca-Ensor
You just need the following propertytrigger:
您只需要以下属性触发器:
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
回答by Scott
You need an EventTrigger
你需要一个 EventTrigger
Give one or both of your Border's GradientStops a name (not the ones in your Trigger):
为 Border 的 GradientStops 中的一个或两个命名(不是触发器中的名称):
<GradientStop Color="#f1f1f1" Offset="1" x:Name="Stop2" />
And add in the following EventTrigger to your ControlTemplate.Triggers:
并将以下 EventTrigger 添加到您的 ControlTemplate.Triggers 中:
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="Stop2" Storyboard.TargetProperty="Color" To="Red" Duration="0" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
If you want to modify both of your gradient stops be sure to give them both a name and perform a ColorAnimation on each one individually (I think you can do them both in the same storyboard)
如果你想修改你的两个渐变停止点,一定要给它们一个名字,并对每个单独执行一个 ColorAnimation(我认为你可以在同一个故事板中做它们)
Hope it helps!
希望能帮助到你!
Edit: This will make the change permanent on a Click event (I tested with VS 2010 Beta 2 and Button.MouseLeftButtonDown doesn't work but Button.Click only works for mouse left button down but not mouse right button down). If you only wish to have the change while the mouse is down... but return to a normal value when the button is no longer pressed... then you should use the IsPressed Property Trigger as noted in the another answer.
编辑:这将使 Click 事件的更改永久化(我使用 VS 2010 Beta 2 进行了测试,Button.MouseLeftButtonDown 不起作用,但 Button.Click 仅适用于鼠标左键按下,而不适用于鼠标右键按下)。如果您只希望在鼠标按下时进行更改...但在不再按下按钮时返回到正常值...那么您应该使用 IsPressed 属性触发器,如另一个答案中所述。