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

时间:2012-12-27 15:21:48

标签: 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.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前景的样式。到目前为止,我有以下风格,但不起作用。它改变了文本块的前景和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>

非常感谢任何有关让它发挥作用的帮助。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

可能为时已晚,但我遇到了同样的问题,并设法解决了这个问题。 只需设置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>
相关问题