如何在wpf中一个接一个地移动两个对象

时间:2017-10-20 07:19:22

标签: c# wpf xaml animation

我是wpf的新手。我试图动画两个文本框。文本框一起移动。如何在文本框之间给出一些时间延迟。我使用了资源。 这是我的xaml代码。

<Window x:Class="Templates.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="rectangle" TargetType="{x:Type TextBox}">

        <Setter Property="RenderTransform">
            <Setter.Value>
                <TranslateTransform/>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <BeginStoryboard>

                    <Storyboard  TargetProperty="(TextBox.RenderTransform).(TranslateTransform.X)">
                        <DoubleAnimation From="-300"
                                         To="300"
                                         Duration="0:0:5"/>
                    </Storyboard>
                </BeginStoryboard> 
            </EventTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<StackPanel x:Name="sp">
    <Button Content="click" Click="Button_Click" VerticalAlignment="Center" Width="50"></Button>
    <!--<Button Content="Click" Click="Button_Click"></Button>-->
    <!--<TextBox x:Name="txt" Width="200" Height="22" Style = "{StaticResource rectangle}"/>-->
</StackPanel>

这是我的代码隐藏部分。这里动态地取两个文本框并将它们分配给资源。给他们相同的资源。这是一个按钮。当我点击按钮时,两个文本框一起移动。我想制作它,它们水平移动相同的路径但是一个接一个。这怎么可能?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Templates
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            for(int i=1;i<=2;i++)
            {

                TextBox tb = new TextBox();
                tb.Width = 70;
                tb.Height = 20;
                tb.Text = "hello" + i;
                tb.SetResourceReference(StyleProperty, "rectangle");
                sp.Children.Add(tb);
               // da.Completed += new EventHandler(completed);
            }

        }




    }
}

0 个答案:

没有答案