这是我的xaml structure
<stackpanel>
<textblock Text="A"></textblock>
<textblock Text="B"></textblock>
</stackpanel>
Stackpanel
,可以循环生成更多相同的结构。
单击文本块时如何更改文本块的背景颜色?
UPDATE: I just want change on xaml file not using C#.
默认情况下我是黄色的,但是当我单击文本块时,它的背景将变为蓝色。
UPDATE 2:如果单击 A,A 将变为蓝色,直到我单击另一个。
Detail:
我点击A(A is yellow)
, A 将变为蓝色 ==> A 为蓝色
我点击B(B is yellow and A is blue)
,B 将变为蓝色,A 将变为黄色。
UPDATE 3: 这个怎么样?
<stackpanel>
<textblock Text="A"></textblock>
</stackpanel>
<stackpanel>
<textblock Text="A"></textblock>
</stackpanel>
<stackpanel>
<textblock Text="A"></textblock>
</stackpanel>
问题和上面一样
Thanks!
Using 事件触发 and 彩色动画您可以更改 TextBlock 背景颜色的颜色鼠标按下 or 鼠标离开
xml代码
<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>
Update
Use 具有属性 IsReadOnly=True 的文本框而不是文本块。
<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)