使用IsMouseOver将文本块的前景设置为按钮的内容

时间:2013-11-06 00:42:39

标签: c# wpf

我希望当您将鼠标悬停在按钮/文本块上时,文本块的前景会发生变化。我试过这个,但这不起作用:

  <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Header}"/>
                                        <Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Margin="5,-3,0, 0" Visibility="{Binding IsCloseButtonVisible}"
                                            Command="{Binding CloseSelectedTabCommand}">
                                            <Button.Content>
                                                <TextBlock Text="x" Foreground="LightGray">
                                                    <TextBlock.Style>
                                                        <Style TargetType="TextBlock">
                                                            <Style.Triggers>
                                                                <Trigger Property="IsMouseOver" Value="True">
                                                                    <Setter Property="Foreground" Value="Red"/>
                                                                </Trigger>
                                                             </Style.Triggers>
                                                            </Style>
                                                        </TextBlock.Style>
                                                </TextBlock>
                                            </Button.Content>
                                        </Button>
                                    </StackPanel>
                                </DataTemplate>

1 个答案:

答案 0 :(得分:2)

您需要在样式中设置前景,而不是在TextBlock中设置它才能工作。

<TextBlock Text="HelloWorld ">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="LightGray"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>