GSP*_*GSP 6 c# wpf xaml textblock
这是我的 xaml structure
<stackpanel>
<textblock Text="A"></textblock>
<textblock Text="B"></textblock>
</stackpanel>
Run Code Online (Sandbox Code Playgroud)
Stackpanel,它可以循环生成更多相同的结构.
单击时如何更改texblock的背景颜色?
更新:I just want change on xaml file not using C#.
默认我有黄色,但是当我单击一个文本块时,它的背景将变为蓝色.
更新2:如果单击A,A将变为蓝色,直到我单击另一个.
详情:
我点击A (A is yellow),A将变为蓝色==> A为蓝色
我点击B (B is yellow and A is blue),B将变为蓝色,A将变为黄色.
更新3:这个怎么样?
<stackpanel>
<textblock Text="A"></textblock>
</stackpanel>
<stackpanel>
<textblock Text="A"></textblock>
</stackpanel>
<stackpanel>
<textblock Text="A"></textblock>
</stackpanel>
Run Code Online (Sandbox Code Playgroud)
问题与上述相同
谢谢!
使用EventTrigger和Color Animation,您可以更改MouseDown或MouseLeave上 TextBlock 背景颜色的颜色
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>
Run Code Online (Sandbox Code Playgroud)
更新
使用属性 IsReadOnly=True 的 TextBox而不是文本块。
<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>
Run Code Online (Sandbox Code Playgroud)