如何更改文本块背景? [英] How to change textblock background?

查看:29
本文介绍了如何更改文本块背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的xaml结构

<stackpanel>
  <textblock Text="A"></textblock>
  <textblock Text="B"></textblock>
</stackpanel>

Stackpanel,可以循环生成更多相同的结构.

Stackpanel, It can loop to generate more same structures.

如何在单击时更改 texblock 的背景颜色?

How can I change background color of texblock when I click it?

更新:我只想更改不使用 C# 的 xaml 文件.

默认我有黄色,但是当我点击一个文本块时,它的背景会变成蓝色.

Default I has yellow, but when I click a textblock, Its background will change to Blue.

更新 2:如果点击 A,A 将变为蓝色,直到我点击另一个.

UPDATE 2: If click A, A will change to blue until I click another.

详情:

  1. 我点击A(A是黄色),A会变成蓝色==> A是蓝色

  1. I click A(A is yellow), A will change to be blue ==> A is blue

我点击B(B是黄色,A是蓝色),B会变成蓝色,A会变成黄色.

I click B(B is yellow and A is blue), B will change to be blue and A will change to be yellow.

更新 3:这个怎么样?

<stackpanel>
      <textblock Text="A"></textblock>
</stackpanel>

<stackpanel>
      <textblock Text="A"></textblock>
</stackpanel>
<stackpanel>
      <textblock Text="A"></textblock>
</stackpanel>

问题同上

谢谢!

推荐答案

使用 EventTrigger颜色动画,您可以在 MouseDown<上更改 TextBlock 背景颜色的颜色/strong> 或 鼠标离开

Using EventTrigger and Color Animation you can change color of TextBlock Background color on MouseDown or MouseLeave

xaml 代码

 <StackPanel>
    <StackPanel.Resources>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <EventTrigger RoutedEvent="MouseDown">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="Background.Color" From="Yellow" To="Blue" Duration="0:0:0.1"></ColorAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="MouseLeave">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="Background.Color" From="Blue" To="Yellow" Duration="0:0:0.1"></ColorAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Resources>
    <TextBlock Text="A" Background="Yellow"></TextBlock>
    <TextBlock Text="B" Background="Yellow"></TextBlock>
</StackPanel>

<小时>

更新

使用 TextBox 和 IsReadOnly=True 属性 而不是 textblock.

Use TextBox with property IsReadOnly=True instead of textblock.

    <StackPanel>
    <StackPanel.Resources>
        <Style TargetType="TextBox"> 
            <Setter Property="Background" Value="Yellow"></Setter>
            <Setter Property="BorderThickness" Value="0"></Setter> 
            <Setter Property="IsReadOnly" Value="True"></Setter>
            <Style.Triggers>
                <EventTrigger RoutedEvent="TextBox.GotFocus">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="Background.Color" From="Yellow" To="Blue" Duration="0:0:0.1"></ColorAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="TextBox.LostFocus">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="Background.Color" From="Blue" To="Yellow" Duration="0:0:0.1"></ColorAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Resources>
    <TextBox Text="A"></TextBox>
    <TextBox Text="A"></TextBox>
</StackPanel>

这篇关于如何更改文本块背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆