C# 你有没有一个解决方案,使前景工程像背景?
请运行下面的代码并将鼠标光标放在TextBlock上,以查看下面的代码在做什么C# 你有没有一个解决方案,使前景工程像背景?,c#,wpf,vb.net,xaml,C#,Wpf,Vb.net,Xaml,请运行下面的代码并将鼠标光标放在TextBlock上,以查看下面的代码在做什么 <Window x:Class="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"> <Grid&
<Window x:Class="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">
<Grid>
<TextBlock Foreground="Blue" Text="www.google.com" Height="20" Width="100">
<TextBlock.Style>
<Style>
<Style.Triggers>
<Trigger Property="TextBlock.IsMouseOver" Value="True">
<Setter Property="TextBlock.Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</Window>
然后从中替换上述代码
<Setter Property="TextBlock.Background" Value="Red"/>
到
看到前景不工作了
您是否有一种解决方案可以使前台像后台一样工作?它不工作的原因是您在
文本块上显式地设置了Foreground=“Blue”
。这将覆盖样式触发器中的任何值。按如下方式更改您的XAML:
<TextBlock Text="www.google.com" Height="20" Width="100">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Blue"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<TextBlock Text="www.google.com" Height="20" Width="100">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Blue"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>