xaml如何在TextBlock上创建一个触发器isMouseHover

时间:2011-04-05 04:57:56

标签: wpf xaml

如何创建触发器,如果​​鼠标悬停该文本块,文本将改变颜色。

2 个答案:

答案 0 :(得分:2)

只需尝试使用背景或前景

<TextBlock Text="Hello" Height="20">   
        <TextBlock.Style>    
            <Style TargetType="TextBlock">      
                <Style.Triggers>         
                    <Trigger Property="IsMouseOver" Value="True">     
                        <Setter Property="TextBlock.Background" Value="red" />   
                    </Trigger>    
                </Style.Triggers>     
            </Style>   
        </TextBlock.Style>
    </TextBlock>

答案 1 :(得分:1)

<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="TextBlockMouseOverStyle" TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <TextBlock Background="Blue" Style="{StaticResource TextBlockMouseOverStyle}" Text="Foo" />
    </Grid>
</Window>