简体   繁体   English

WPF:更改鼠标左键下边框的背景颜色

[英]WPF: Change background color of border on left mouse button down

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.现在,当用户用鼠标左键单击按钮时,我尝试更改名称为"Background"Border元素的背景颜色。

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>

You just need the following property trigger:您只需要以下属性触发器:

<ControlTemplate.Triggers>
    <Trigger Property="IsPressed" Value="True">
        <Setter Property="Background" TargetName="Background" Value="Red"/>
    </Trigger>
</ControlTemplate.Triggers>

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).编辑:这将使 Click 事件的更改永久化(我使用 VS 2010 Beta 2 进行了测试,Button.MouseLeftButtonDown 不起作用,但 Button.Click 仅适用于鼠标左键按下,而不适用于鼠标右键按下)。 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.如果您只希望在鼠标按下时进行更改...但在不再按下按钮时返回到正常值...那么您应该使用 IsPressed 属性触发器,如另一个答案中所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM