C# 在鼠标上方更改按钮文本和图像的前景

C# 在鼠标上方更改按钮文本和图像的前景,c#,wpf,xaml,wpf-controls,C#,Wpf,Xaml,Wpf Controls,我有一个按钮定义如下: <Button x:Name="ContactLink" Canvas.Left="20" Canvas.Top="223" Style="{StaticResource HyperlinkButton}" Click="ContactLink_Clicked"> <WrapPanel> <Rectangle Width="15" Height="15"> <Rectangle.F

我有一个按钮定义如下:

<Button x:Name="ContactLink" Canvas.Left="20" Canvas.Top="223" Style="{StaticResource HyperlinkButton}" Click="ContactLink_Clicked">
    <WrapPanel>
        <Rectangle Width="15" Height="15">
             <Rectangle.Fill>
                 <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_new_window}" />
             </Rectangle.Fill>
        </Rectangle>
        <TextBlock Margin="5 0 0 0" FontWeight="SemiBold" Text="GET IN TOUCH" />
     </WrapPanel>
</Button>
正如标题所说,我想定义一种样式,它可以改变TextBlock和VisualBrush的前景。到目前为止,我有以下的风格,这是行不通的。它改变了textblock的前景和BlackBrush资源的定义

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ControlTemplate x:Key="HyperlinkButtonTemplate" TargetType="{x:Type Button}">
         <WrapPanel>
             <TextBlock Cursor="Hand">
                  <ContentPresenter />
             </TextBlock>
         </WrapPanel>
         <ControlTemplate.Triggers>
             <Trigger Property="Button.IsMouseOver" Value="True">
                 <Setter Property="TextBlock.Foreground" Value="#0071bc" />
             </Trigger>
         </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="HyperlinkButton" TargetType="{x:Type Button}">
        <Setter Property="Template" Value="{StaticResource HyperlinkButtonTemplate}" />
    </Style>
 </ResourceDictionary>

如果您能帮助它工作,我们将不胜感激。

您只需删除在触发器中定义的SolidColorBrush即可

<Trigger Property="Button.IsMouseOver" Value="True">
              <Setter Property="TextBlock.Foreground" Value="#0071bc" />
 </Trigger>

可能太晚了,但我遇到了同样的问题,并设法解决了它。 只需设置TextBlock的name属性,就可以在触发器中引用该名称

<TextBlock Cursor="Hand" Name="MyTextBlock">
          <ContentPresenter />
</TextBlock>

<ControlTemplate.Triggers>
             <Trigger Property="Button.IsMouseOver" Value="True">
                <Setter TargetName="MyTextBlock" Property="Foreground" Value="#0071bc"/>
             </Trigger>
</ControlTemplate.Triggers>

我按照你的建议修改了样式,但是颜色还是没有改变。对我来说很有用!鼠标上的前景是否会发生变化?视觉画笔是否有矢量图像?是。它有一个矢量图像,可以在这里找到:为了更清楚,您想根据前景色更改矢量图像的颜色吗?是的,当用户将鼠标悬停在按钮控件上时,我想更改矢量图像和文本的颜色。SVG图像使用名为BlackBrush的资源来绘制自身。