WPF - 超链接样式不随内部标签的样式而改变
鉴于以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您设置的是超链接的样式,而不是标签。您需要为标签设置相同的触发器,以便它也可以对 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.
@Fredrik 上面解释得很好。所以这里可以是一个简单的样式和超链接用法
你应该像这样构建你的超链接
然后这种样式应该适合你的正常和悬停样式
@Fredrik explained well above. So here can be a simple style and Hyperlink usage
You should build your Hyperlink like this
And then this style should work for you with normal and hover styles
似乎
Label
不受其父级上设置的Foreground
的影响。即使这样也没有效果更新
为
Label
而不是Hyperlink
设置样式,它就可以再次更新
最简单的方法是使用
TextBlock
而不是Label
因为它没有这个问题It seems that
Label
isn't affected byForeground
set on its parent. Even this has no effectUpdate
Set a Style for the
Label
instead of theHyperlink
and it'll workUpdate again
The easy way is to use a
TextBlock
instead ofLabel
since it doesn't have this problem