前端代码
1 <Window x:Class="WpfApp2.Window3" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp2" 7 mc:Ignorable="d" 8 Title="Window3" Height="450" Width="800"> 9 <Grid> 10 <StackPanel> 11 <TextBlock Margin="5" 12 Text="test" 13 FontSize="28" 14 FontWeight="Bold"> 15 <TextBlock.Foreground> 16 <SolidColorBrush Color="Black"/> 17 </TextBlock.Foreground> 18 <TextBlock.Style> 19 <Style TargetType="TextBlock"> 20 <Style.Triggers> 21 <EventTrigger RoutedEvent="TextBlock.MouseEnter"> 22 <EventTrigger.Actions> 23 <BeginStoryboard Name="aa"> 24 <Storyboard> 25 <ColorAnimation From="Black" 26 To="Red" 27 RepeatBehavior="Forever" 28 Duration="0:0:0.5" 29 Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" /> 30 </Storyboard> 31 </BeginStoryboard> 32 </EventTrigger.Actions> 33 </EventTrigger> 34 <EventTrigger RoutedEvent="TextBlock.MouseLeave"> 35 <EventTrigger.Actions> 36 <StopStoryboard BeginStoryboardName="aa"/> 37 </EventTrigger.Actions> 38 </EventTrigger> 39 40 <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:Window3},Path=IsSelected}" Value="true"> 41 <DataTrigger.EnterActions> 42 <BeginStoryboard Name="bb"> 43 <Storyboard> 44 <ColorAnimation From="Green" 45 To="Yellow" 46 RepeatBehavior="Forever" 47 Duration="0:0:0.5" 48 Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" /> 49 </Storyboard> 50 </BeginStoryboard> 51 </DataTrigger.EnterActions> 52 </DataTrigger> 53 <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:Window3},Path=IsSelected}" Value="false"> 54 <DataTrigger.EnterActions> 55 <StopStoryboard BeginStoryboardName="bb" /> 56 </DataTrigger.EnterActions> 57 </DataTrigger> 58 </Style.Triggers> 59 </Style> 60 </TextBlock.Style> 61 </TextBlock> 62 </StackPanel> 63 </Grid> 64 </Window>
效果已经实现,后端代码协助调试,变量的绑定而已,模拟现实操心,2秒后绑定值变动
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6 using System.Threading.Tasks; 7 using System.Windows; 8 using System.Windows.Controls; 9 using System.Windows.Data; 10 using System.Windows.Documents; 11 using System.Windows.Input; 12 using System.Windows.Media; 13 using System.Windows.Media.Imaging; 14 using System.Windows.Shapes; 15 16 namespace WpfApp2 17 { 18 /// <summary> 19 /// Window3.xaml 的交互逻辑 20 /// </summary> 21 public partial class Window3 : Window 22 { 23 public Window3() 24 { 25 InitializeComponent(); 26 27 Thread th = new System.Threading.Thread(() => 28 { 29 while (true) 30 { 31 Thread.Sleep(2000); 32 Dispatcher.Invoke(() => 33 { 34 IsSelected = true; 35 }); 36 Thread.Sleep(3000); 37 Dispatcher.Invoke(() => 38 { 39 IsSelected = false; 40 }); 41 } 42 }); 43 th.IsBackground = true; 44 th.Start(); 45 } 46 47 public bool IsSelected 48 { 49 get { return (bool)GetValue(IsSelectedProperty); } 50 set { SetValue(IsSelectedProperty, value); } 51 } 52 53 public static readonly DependencyProperty IsSelectedProperty = 54 DependencyProperty.Register("IsSelected", typeof(bool), typeof(Window3), new PropertyMetadata(false)); 55 } 56 }