我有2个文本框,textbox1和textbox2 我想通过此代码从左到右移动textbox1和textbox2 我使用Thread.Sleep使它看起来像2个文本框正在移动,但它没有工作 请帮我解决一下,这是我的代码
double a = textBox1.Margin.Right;
double b = textBox2.Margin.Right;
double c = textBox1.Margin.Top;
double f = textBox2.Margin.Top;
textBox1.Text = textBox2.Margin.Left.ToString() + "," + textBox2.Margin.Top.ToString();
textBox2.Text = textBox1.Margin.Left.ToString() + "," + textBox1.Margin.Top.ToString();
while(c < f )
{
textBox1.Margin = new Thickness(textBox1.Margin.Left, textBox1.Margin.Top, a+=10, textBox1.Margin.Bottom);
textBox2.Margin = new Thickness(textBox2.Margin.Left, textBox2.Margin.Top, b+=10, textBox2.Margin.Bottom);
c += 10;
Thread.Sleep(20);
}
答案 0 :(得分:0)
你应该在故事板中特别使用DoubleAnimation动画
示例:
<StackPanel>
<StackPanel.Resources>
<!-- Animates the rectangle's opacity. -->
<Storyboard x:Name="myStoryboard">
<DoubleAnimation
Storyboard.TargetName="MyAnimatedRectangle"
Storyboard.TargetProperty="Opacity"
From="1.0" To="0.0" Duration="0:0:1"
AutoReverse="True"/>
</Storyboard>
</StackPanel.Resources>
<Rectangle MouseLeftButtonUp="Rectangle_Tapped"
x:Name="MyAnimatedRectangle"
Width="300" Height="200" Fill="Blue" />
</StackPanel>
对于您的特定情况,我建议您为TranslateTransform.X和.Y属性设置动画,而不是在此处设置边距:
<StackPanel>
<StackPanel.Resources>
<!-- Animates the rectangle's opacity. -->
<Storyboard x:Name="myStoryboard">
<DoubleAnimation
Storyboard.TargetName="rectangle1TranslateTransform"
Storyboard.TargetProperty="X"
From="0" To="100" Duration="0:0:1.50"
AutoReverse="True"/>
</Storyboard>
</StackPanel.Resources>
<TextBlock MouseLeftButtonUp="Rectangle_Tapped"
Width="300" Height="200" Fill="Blue" >
<TextBlock.RenderTransform>
<TranslateTransform x:Name="rectangle1TranslateTransform"/>
</TextBlock.RenderTransform>
</TextBlock>
</StackPanel>