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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml [WPF]コードビハインド侧でアニメーション制御.DataGridの行を実行时にアニメーション制御。相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using WpfApplication1.Annotations;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        private ColorAnimation _color1;
        private ColorAnimation _color2;


        public MainWindow()
        {
            InitializeComponent();
        }

        private void Btn1_OnClick(object sender, RoutedEventArgs e)
        {
            var anim = new ColorAnimation();
            anim.To = Colors.Red;
            anim.Duration = new Duration(TimeSpan.FromSeconds(3));

            var brush = new SolidColorBrush();
            txb1.Background = brush;
            brush.BeginAnimation(SolidColorBrush.ColorProperty, anim);
        }

        private void Btn2_OnClick(object sender, RoutedEventArgs e)
        {
        }

        private void GrdMain_AddingNewItem(object sender, AddingNewItemEventArgs e)
        {
            Debugger.Break();
        }

        private void GrdMain_InitializingNewItem(object sender, InitializingNewItemEventArgs e)
        {
            Debugger.Break();
        }

        private void GrdMain_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            Debugger.Break();
        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            var vm = DataContext as MyModel;
            GrdMain.ItemsSource = vm.MyItems;

            _color1 = new ColorAnimation();
            _color1.To = Colors.Blue;
            _color1.Duration = new Duration(TimeSpan.FromSeconds(2));
            _color1.AutoReverse = true;
            _color1.RepeatBehavior = RepeatBehavior.Forever;

            _color2 = new ColorAnimation();
            _color2.To = Colors.Red;
            _color2.Duration = new Duration(TimeSpan.FromSeconds(2));
            _color2.AutoReverse = true;
            _color2.RepeatBehavior = RepeatBehavior.Forever;
        }

        private void GrdMain_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            var row = e.Row;
            var data = row.Item as MyData;

            var brush = new SolidColorBrush();
            row.Background = brush;

            if (data == null)
            {
                return;
            }

            if (data.MyName == "hoge1")
            {
                brush.BeginAnimation(SolidColorBrush.ColorProperty, _color1);
            }
            else
            {
                brush.BeginAnimation(SolidColorBrush.ColorProperty, _color2);
            }
        }
    }

    public class MyModel : INotifyPropertyChanged
    {
        private ObservableCollection<MyData> _myItems;
        public event PropertyChangedEventHandler PropertyChanged;

        public MyModel()
        {
            MyItems = new ObservableCollection<MyData>();
            MyItems.Add(new MyData {MyName = "hoge1", MyAge = 30});
            MyItems.Add(new MyData {MyName = "hoge2", MyAge = 33});
        }

        public ObservableCollection<MyData> MyItems
        {
            get { return _myItems; }
            set
            {
                if (Equals(value, _myItems)) return;
                _myItems = value;
                OnPropertyChanged();
            }
        }

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class MyData : INotifyPropertyChanged
    {
        private string _myName;
        private int _myAge;
        public event PropertyChangedEventHandler PropertyChanged;

        public string MyName
        {
            get { return _myName; }
            set
            {
                if (value == _myName) return;
                _myName = value;
                OnPropertyChanged();
            }
        }

        public int MyAge
        {
            get { return _myAge; }
            set
            {
                if (value == _myAge) return;
                _myAge = value;
                OnPropertyChanged();
            }
        }

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="MainWindow_OnLoaded">
    <Window.DataContext>
        <local:MyModel />
    </Window.DataContext>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        
        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <Button x:Name="btn1" Content="アニメーション開始" Click="Btn1_OnClick"></Button>
            <TextBlock x:Name="txb1" Text="色が変わる対象"></TextBlock>
            <Button x:Name="btn2" Content="DataGridの行に対してアニメーション" Click="Btn2_OnClick"></Button>
        </StackPanel>

        <DataGrid x:Name="GrdMain" Grid.Row="1" AutoGenerateColumns="False" AddingNewItem="GrdMain_AddingNewItem" InitializingNewItem="GrdMain_InitializingNewItem" DataContextChanged="GrdMain_DataContextChanged" LoadingRow="GrdMain_LoadingRow">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding MyName}"></DataGridTextColumn>
                <DataGridTextColumn Header="Age" Binding="{Binding MyAge}"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

以上是关于xml [WPF]コードビハインド侧でアニメーション制御.DataGridの行を実行时にアニメーション制御。的主要内容,如果未能解决你的问题,请参考以下文章

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

javascript 20180507アニメーションで淡出,淡出(ハンバーガー)

scss アコーディオン:リスト型:アニメーション

html ボタンアニメーション

css CSSアニメーション

css ホバー时矢印が横へ移动アニメーション