WPF - 超链接样式不随内部标签的样式而改变

Ant*_*ony 5 wpf label styles hyperlink

鉴于以下 XAML 标记,当我将鼠标悬停在超链接上时,我希望超链接中的文本变为橙色,因为我在其父控件上设置前景色,并且它应该通过Property Value Inheritance进行过滤。但它仍然是黑色的。我需要做什么?

<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>
Run Code Online (Sandbox Code Playgroud)


更新: Meleak 的简单答案是,使用 TextBlock 而不是内部 Label 会导致样式按预期工作 - TextBlock 从其父级获取前景色,而 Label 则不会。

例如

<Label>
    <Hyperlink Style="{StaticResource DemoLink}">
        <TextBlock Text="Text that does change colour on mouse over" />
    </Hyperlink>
</Label>
Run Code Online (Sandbox Code Playgroud)

Fre*_*lad 3

似乎Label不受Foreground其父级设置的影响。即使这样也没有效果

<Label>
    <Hyperlink Style="{StaticResource DemoLink}" Foreground="DarkOrange">
        <Label Content="This is some text that should change colour on mouse over" />
    </Hyperlink>
</Label>
Run Code Online (Sandbox Code Playgroud)

更新
为 the 设置样式Label而不是 the 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>
Run Code Online (Sandbox Code Playgroud)

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

<Hyperlink Name="DemoHyperlink" Style="{StaticResource DemoLink}">
    <TextBlock Text="This is some text that should change colour on mouse over"/>
</Hyperlink>
Run Code Online (Sandbox Code Playgroud)