csharp [WPF] DataGridの行の点灭。パターンごとに同じアニメーションを同じタイミングで実施。(ParallelTimeline,ClockGroup,ApplyAnimationCl

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp [WPF] DataGridの行の点灭。パターンごとに同じアニメーションを同じタイミングで実施。(ParallelTimeline,ClockGroup,ApplyAnimationCl相关的知识,希望对你有一定的参考价值。

<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>
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));
        }
    }
}

以上是关于csharp [WPF] DataGridの行の点灭。パターンごとに同じアニメーションを同じタイミングで実施。(ParallelTimeline,ClockGroup,ApplyAnimationCl的主要内容,如果未能解决你的问题,请参考以下文章

xml [WPF]コードビハインド侧でアニメーション制御.DataGridの行を実行时にアニメーション制御。

csharp 【WPF】DataGrid中のサンプル

wpf datagrid怎么得到焦点?用datagrid.focus()不行

wpf datagrid 多行表头

wpf datagrid 怎么增加数据行

wpf datagrid cell 设置焦点