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

WPF-超链接样式不会随内部标签的样式而更改,wpf,hyperlink,label,styles,Wpf,Hyperlink,Label,Styles,考虑到下面的XAML标记,我希望当我将鼠标移到超链接上时,超链接中的文本会变成橙色,因为我正在其父控件上设置前景颜色,并且它应该按颜色过滤。但它仍然是黑色的。我需要做什么 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.

考虑到下面的XAML标记,我希望当我将鼠标移到超链接上时,超链接中的文本会变成橙色,因为我正在其父控件上设置前景颜色,并且它应该按颜色过滤。但它仍然是黑色的。我需要做什么

<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从其父级拾取前景颜色,而标签则不会

e、 g



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

似乎
标签
不受其父项上设置的
前景
的影响。即使这样也没有效果

<Label>
    <Hyperlink Style="{StaticResource DemoLink}" Foreground="DarkOrange">
        <Label Content="This is some text that should change colour on mouse over" />
    </Hyperlink>
</Label>
再次更新
简单的方法是使用
文本块
而不是
标签
,因为它没有这个问题

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

@Fredrik在上面解释得很清楚。所以这里可以是一个简单的样式和超链接用法

你应该像这样建立你的超链接

<TextBlock Width="Auto" HorizontalAlignment="Center">
    <Hyperlink Click="ForgotPassword_Clicked">
        <TextBlock Text="Forgot Password?"/>
    </Hyperlink>
</TextBlock>

然后这种样式应该适用于普通样式和悬停样式

<Style TargetType="{x:Type Hyperlink}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Foreground" Value="Blue" />
        <Setter Property="TextBlock.TextDecorations" Value="{x:Null}" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Red" />
                <Setter Property="TextBlock.TextDecorations" Value="Underline" />
            </Trigger>
        </Style.Triggers>
    </Style>


是的,我设置了超链接的样式,而不是标签。标签没有显式设置前景,因此它应该根据属性继承从其父级获取该值。我希望超链接中的所有内容都获得该前景集。但事实并非如此,不是每个依赖属性都参与值继承。超链接,我相信是一个没有。我可以看出你们找到了TextBlock的解决方案,但我可以再提一种方法。您可以这样写:如果超链接不参与值继承,这是一些应该在鼠标上改变颜色的文本,那么前景属性如何从超链接传递到textBlock?更可能的情况是Label.Foreground属性没有继承值,因为它是由标签的默认样式显式设置的,这具有优先权,正如Meleak所说。这是可行的,但我不满意。如果超链接中有两个标签怎么办?将鼠标悬停在超链接的任何部分上都会设置所有超链接的颜色。@Anthony。发现了问题。在标签的默认样式中,前景设置为。我会试着想出一个解决问题的办法it@Anthony:我用
文本块
而不是
标签
更新了我的答案。您必须使用
标签吗
?非常感谢!这正好给了我想要的解决方案,没有红色的前景。
<TextBlock Width="Auto" HorizontalAlignment="Center">
    <Hyperlink Click="ForgotPassword_Clicked">
        <TextBlock Text="Forgot Password?"/>
    </Hyperlink>
</TextBlock>
<Style TargetType="{x:Type Hyperlink}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Foreground" Value="Blue" />
        <Setter Property="TextBlock.TextDecorations" Value="{x:Null}" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Red" />
                <Setter Property="TextBlock.TextDecorations" Value="Underline" />
            </Trigger>
        </Style.Triggers>
    </Style>