Loading

WPF-不同内容更新字体颜色

    <StackPanel Margin="10">
        <!--  输入框  -->
        <TextBox
            x:Name="InputBox"
            Margin="0,0,0,10"
            Text="Initial Text" />

        <!--  显示文本的控件(绑定到输入框的文本)  -->
        <TextBlock
            x:Name="DisplayText"
            HorizontalAlignment="Center"
            FontSize="16"
            Text="{Binding ElementName=InputBox, Path=Text, NotifyOnTargetUpdated=True}">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <!--  默认颜色  -->
                    <Setter Property="Foreground" Value="Black" />
                    <Style.Triggers>
                        <!--  当 Text 更新时触发动画  -->
                        <EventTrigger RoutedEvent="Binding.TargetUpdated">
                            <BeginStoryboard>
                                <Storyboard>
                                    <!--  动画:红色闪烁后恢复黑色  -->
                                    <ColorAnimation
                                        AutoReverse="True"
                                        Storyboard.TargetProperty="Foreground.Color"
                                        From="Black"
                                        To="Red"
                                        Duration="0:0:0.3" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
        <Button Click="textCmd" Content="Text" />
        <Button Click="textCmd" Content="Text1" />
    </StackPanel>
  private void textCmd(object sender, RoutedEventArgs e)
  {
      var text = (sender as Button).Content.ToString();
      InputBox.Text = text;
  }
posted @ 2025-05-09 11:37  androllen  阅读(66)  评论(0)    收藏  举报