WPF编程,通过定时器动态更改控件属性的一种方法。

版权声明:我不生产代码,我只是代码的搬运工。 https://blog.csdn.net/qq_43307934/article/details/87249558

一种通过定时器来实现前台动画的一方法。这是动态调整一个方块的宽度。

1、增加using 

using System.Windows.Threading;


2、前台

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height=" *" />
            <RowDefinition Height=" *" />
        </Grid.RowDefinitions>
        <Rectangle Name="rectan"
                   Width=" 30"
                   Height=" 100"
                   Fill="Blue" />
        <Button Grid.Row=" 1"
                Width=" 100"
                Height=" 30"
                Click="Button_Click" />
    </Grid>

3、后台.cs

    public partial class MainWindow : Window
    {
        private double maxwi = 100;
        private double strart = 0;


        public MainWindow()
        {
            InitializeComponent();
            strart = rectan.Width;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DispatcherTimer tmr = new DispatcherTimer();
            tmr.Interval = TimeSpan.FromSeconds(1);
            tmr.Tick += Timeontick;
            tmr.Start();
        }

        private void Timeontick(object sender, EventArgs e)
        {
            rectan.Width += 1;
            if (rectan.Width>maxwi)
            {
                rectan.Width = strart;
                (sender as DispatcherTimer).Stop();
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_43307934/article/details/87249558