Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 你有没有一个解决方案,使前景工程像背景?_C#_Wpf_Vb.net_Xaml - Fatal编程技术网

C# 你有没有一个解决方案,使前景工程像背景?

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&

请运行下面的代码并将鼠标光标放在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>
    <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>