2011-02-01 55 views
1

鉴于以下XAML标记,我希望超链接中的文本在我将鼠标移到它上方时变成橙色,因为我在其父控件上设置了前景色它应该过滤掉Property Value Inheritance。但它保持黑色。我需要做什么?WPF - 超链接风格不会随着标签内部样式的变化而变化

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <Style x:Key="DemoLink" TargetType="{x:Type Hyperlink}"> 
      <Style.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 
        <Setter Property="Foreground" Value="DarkOrange" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <Label> 
      <Hyperlink Style="{StaticResource DemoLink}"> 
       <Label Content="Text that should change colour on mouse over" /> 
      </Hyperlink> 
     </Label> 
    </Grid> 
</Window> 


更新:从Meleak简单的答案是,使用TextBlock的,而不是内标签会导致如预期的那样风格的工作 - 将TextBlock拿起从其父前景色,而标签才不是。

例如

<Label> 
    <Hyperlink Style="{StaticResource DemoLink}"> 
     <TextBlock Text="Text that does change colour on mouse over" /> 
    </Hyperlink> 
</Label> 

回答

2

似乎Label不受Foreground设置在其父项。即使这没有任何影响

<Label> 
    <Hyperlink Style="{StaticResource DemoLink}" Foreground="DarkOrange"> 
     <Label Content="This is some text that should change colour on mouse over" /> 
    </Hyperlink> 
</Label> 

更新
设置样式为Label,而不是Hyperlink的,它会工作

<Window.Resources> 
    <Style x:Key="DemoLinkLabel" TargetType="Label"> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="Foreground" Value="DarkOrange" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<Grid> 
    <Label> 
     <Hyperlink Name="DemoHyperlink" > 
      <Label Content="This is some text that should change colour on mouse over" 
        Style="{StaticResource DemoLinkLabel}"/> 
     </Hyperlink> 
    </Label> 
</Grid> 

再次更新
最简单的方法是使用TextBlock而不是Label,因为它没有这个问题

<Hyperlink Name="DemoHyperlink" Style="{StaticResource DemoLink}"> 
    <TextBlock Text="This is some text that should change colour on mouse over"/> 
</Hyperlink> 
0

您已设置超链接样式,而不是标签。您需要为标签设置相同的触发器,因此它也可以对IsMouseOver事件做出反应。

+0

是的,我设置了超链接的样式,而不是标签。标签没有明确设置前景,所以它应该根据属性继承从其父项中获取该值http://msdn.microsoft.com/en-us/library/ms753197.aspx我希望超链接中的所有内容得到那个forground集合。但事实并非如此。 – Anthony 2011-02-01 23:00:01

+1

并非每个依赖属性都参与价值继承。超链接,我相信是一个没有。我可以看到你找到了TextBlock的解决方案,但我可以提到更多的方法来做到这一点。你可以写:<超链接样式=“{StaticResource DemoLink}”> 这是一些文字,应该改变鼠标的颜色 – ShaQ 2011-02-02 11:58:48

相关问题