Created
January 20, 2016 11:04
-
-
Save devlights/f80b2ac34ad64cc8e0ab to your computer and use it in GitHub Desktop.
[WPF] DataGridの行の点滅。パターンごとに同じアニメーションを同じタイミングで実施。(ParallelTimeline, ClockGroup, ApplyAnimationClock)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace WpfApplication6 | |
| { | |
| using System.Linq; | |
| using System.Windows; | |
| using System.Windows.Controls; | |
| using System.Windows.Media; | |
| using System.Collections.ObjectModel; | |
| using System.ComponentModel; | |
| using System.Runtime.CompilerServices; | |
| using System.Windows.Media.Animation; | |
| using Annotations; | |
| public partial class MainWindow : Window | |
| { | |
| private ClockGroup _clockGroup; | |
| public MainWindow() | |
| { | |
| InitializeComponent(); | |
| } | |
| private void MyGrid_OnLoadingRow(object sender, DataGridRowEventArgs e) | |
| { | |
| if (_clockGroup == null) | |
| { | |
| var pt = FindResource("p") as ParallelTimeline; | |
| _clockGroup = pt.CreateClock(); | |
| } | |
| var i = e.Row.Item as Item; | |
| if (i == null) | |
| { | |
| return; | |
| } | |
| i.PropertyChanged += (o, args) => | |
| { | |
| if (args.PropertyName == "BlinkMode") | |
| { | |
| if (i.BlinkMode == BlinkMode.Slow) | |
| { | |
| e.Row.Background.ApplyAnimationClock(SolidColorBrush.ColorProperty, _clockGroup.Children[0] as AnimationClock); | |
| } | |
| else if (i.BlinkMode == BlinkMode.Fast) | |
| { | |
| e.Row.Background.ApplyAnimationClock(SolidColorBrush.ColorProperty, _clockGroup.Children[1] as AnimationClock); | |
| } | |
| else | |
| { | |
| e.Row.Background.ApplyAnimationClock(SolidColorBrush.ColorProperty, null); | |
| } | |
| } | |
| }; | |
| e.Row.Background = new SolidColorBrush(); | |
| if (i.BlinkMode == BlinkMode.Slow) | |
| { | |
| e.Row.Background.ApplyAnimationClock(SolidColorBrush.ColorProperty, _clockGroup.Children[0] as AnimationClock); | |
| } | |
| else if (i.BlinkMode == BlinkMode.Fast) | |
| { | |
| e.Row.Background.ApplyAnimationClock(SolidColorBrush.ColorProperty, _clockGroup.Children[1] as AnimationClock); | |
| } | |
| } | |
| private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) | |
| { | |
| } | |
| private void Hoge1_OnClick(object sender, RoutedEventArgs e) | |
| { | |
| var vm = DataContext as MyVM; | |
| vm.Items.Add(new Item{ Value1 = "Blink", Value2 = "Fast", BlinkMode = BlinkMode.Fast}); | |
| } | |
| private void Hoge2_OnClick(object sender, RoutedEventArgs e) | |
| { | |
| var vm = DataContext as MyVM; | |
| vm.Items.Add(new Item { Value1 = "Blink", Value2 = "Slow", BlinkMode = BlinkMode.Slow }); | |
| } | |
| private void Hoge3_OnClick(object sender, RoutedEventArgs e) | |
| { | |
| var vm = DataContext as MyVM; | |
| vm.Items.First().BlinkMode = BlinkMode.None; | |
| } | |
| } | |
| public class MyVM | |
| { | |
| public MyVM() | |
| { | |
| Items = new ObservableCollection<Item>(); | |
| Items.Add(new Item { Value1 = "hello", Value2 = "world", BlinkMode = BlinkMode.Fast}); | |
| Items.Add(new Item { Value1 = "world", Value2 = "hello", BlinkMode = BlinkMode.Slow}); | |
| Items.Add(new Item { Value1 = "HELLO", Value2 = "WORLD", BlinkMode = BlinkMode.None}); | |
| } | |
| public ObservableCollection<Item> Items { get; set; } | |
| } | |
| public enum BlinkMode | |
| { | |
| Fast, | |
| Slow, | |
| None | |
| } | |
| public class Item : INotifyPropertyChanged | |
| { | |
| public event PropertyChangedEventHandler PropertyChanged; | |
| private string _value1; | |
| private string _value2; | |
| private BlinkMode _blinkMode; | |
| public string Value1 | |
| { | |
| get { return _value1; } | |
| set | |
| { | |
| if (value == _value1) return; | |
| _value1 = value; | |
| OnPropertyChanged(); | |
| } | |
| } | |
| public string Value2 | |
| { | |
| get { return _value2; } | |
| set | |
| { | |
| if (value == _value2) return; | |
| _value2 = value; | |
| OnPropertyChanged(); | |
| } | |
| } | |
| public BlinkMode BlinkMode | |
| { | |
| get { return _blinkMode; } | |
| set | |
| { | |
| if (value == _blinkMode) return; | |
| _blinkMode = value; | |
| OnPropertyChanged(); | |
| } | |
| } | |
| [NotifyPropertyChangedInvocator] | |
| protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) | |
| { | |
| var handler = PropertyChanged; | |
| if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Window x:Class="WpfApplication6.MainWindow" | |
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
| xmlns:wpfApplication6="clr-namespace:WpfApplication6" | |
| Title="MainWindow" Height="350" Width="813" Loaded="MainWindow_OnLoaded"> | |
| <Window.DataContext> | |
| <wpfApplication6:MyVM /> | |
| </Window.DataContext> | |
| <Window.Resources> | |
| <ParallelTimeline x:Key="p"> | |
| <ColorAnimationUsingKeyFrames RepeatBehavior="Forever" Duration="0:0:2" AutoReverse="True" | |
| FillBehavior="Stop"> | |
| <EasingColorKeyFrame Value="Red" KeyTime="0:0:1"></EasingColorKeyFrame> | |
| <EasingColorKeyFrame Value="Transparent" KeyTime="0:0:2"></EasingColorKeyFrame> | |
| </ColorAnimationUsingKeyFrames> | |
| <ColorAnimationUsingKeyFrames RepeatBehavior="Forever" Duration="0:0:1" AutoReverse="True" | |
| FillBehavior="Stop"> | |
| <EasingColorKeyFrame Value="LightBlue" KeyTime="0:0:0.500"></EasingColorKeyFrame> | |
| <EasingColorKeyFrame Value="Transparent" KeyTime="0:0:1"></EasingColorKeyFrame> | |
| </ColorAnimationUsingKeyFrames> | |
| </ParallelTimeline> | |
| </Window.Resources> | |
| <Grid> | |
| <Grid.RowDefinitions> | |
| <RowDefinition Height="Auto"></RowDefinition> | |
| <RowDefinition Height="*"></RowDefinition> | |
| </Grid.RowDefinitions> | |
| <StackPanel Grid.Row="0" Orientation="Horizontal"> | |
| <Button x:Name="Hoge1" Click="Hoge1_OnClick" Content="点滅データを1件追加(Fast)"></Button> | |
| <Button x:Name="Hoge2" Click="Hoge2_OnClick" Content="点滅データを1件追加(Slow)"></Button> | |
| <Button x:Name="Hoge3" Click="Hoge3_OnClick" Content="1行目をNoneに"></Button> | |
| </StackPanel> | |
| <DataGrid x:Name="MyGrid" Grid.Row="1" AutoGenerateColumns="True" ItemsSource="{Binding Items}" | |
| LoadingRow="MyGrid_OnLoadingRow" Grid.ColumnSpan="2" RowBackground="Transparent"> | |
| </DataGrid> | |
| </Grid> | |
| </Window> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment