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

发布于 2024-10-15 06:09:16 字数 1600 浏览 1 评论 0原文

鉴于以下 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>


Update: The simple answer from Meleak is that using a TextBlock instead of the inner Label causes the style to work as expected - the TextBlock picks up the foreground colour from its parent, while the Label does not.

例如

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

Given the following XAML markup, I would expect the text in the Hyperlink to turn orange when I mouse over it, since I am setting a foreground colour on its parent control and it should filter down by Property Value Inheritance. Yet it stays black. What do I need to do?

<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>


Update: The simple answer from Meleak is that using a TextBlock instead of the inner Label causes the style to work as expected - the TextBlock picks up the foreground colour from its parent, while the Label does not.

e.g.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

━╋う一瞬間旳綻放 2024-10-22 06:09:17

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

You have set the style for hyperlink, not the label. You need to set the same trigger for a label, so it can also react to IsMouseOver event.

野の 2024-10-22 06:09:16

@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>

@Fredrik explained well above. So here can be a simple style and Hyperlink usage

You should build your Hyperlink like this

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

And then this style should work for you with normal and hover styles

<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>
眼睛会笑 2024-10-22 06:09:16

似乎 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>

It seems that Label isn't affected by Foreground set on its parent. Even this has no effect

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

Update
Set a Style for the Label instead of the Hyperlink and it'll work

<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>

Update again
The easy way is to use a TextBlock instead of Label since it doesn't have this problem

<Hyperlink Name="DemoHyperlink" Style="{StaticResource DemoLink}">
    <TextBlock Text="This is some text that should change colour on mouse over"/>
</Hyperlink>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文