我想制作动画:
基本上我只想从右到左进行TextBlock
移动。
这是XAML文件:
<Window x:Class="WpfApplication1.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">
<Grid>
<Grid x:Name="grdTextblockParent">
<Grid.RenderTransform>
<TranslateTransform x:Name="grdTransform"/>
</Grid.RenderTransform>
<TextBlock Text="This is a Test 1This is a Test 2This is a Test 3This is a Test 4This is a Test 5This is a Test 6This is a Test 7This is a Test 8This is a Test 9This is a Test 10"></TextBlock>
</Grid>
<Button Width="75" Height="50" Content="Start" Click="Button_Click"/>
</Grid>
</Window>
如你所见,TextBlock有很多:&#34;这是一个测试*(数字)&#34;。 现在我在后台添加动画。
private DoubleAnimationUsingKeyFrames animationLongMsg = new DoubleAnimationUsingKeyFrames(); // Animation to move it
public MainWindow()
{
InitializeComponent();
// move it to the right, at the begin.
(grdTextblockParent.RenderTransform as TranslateTransform).X = 1000;
}
// after click , move the animation
private void Button_Click(object sender, RoutedEventArgs e)
{
animationLongMsg.KeyFrames.Add(new LinearDoubleKeyFrame(-1000, new TimeSpan(0, 0, 10)));
grdTextblockParent.RenderTransform.BeginAnimation(TranslateTransform.XProperty, animationLongMsg);
}
现在一切都很好。动画很棒。但问题是, TextBlock文本不能完全显示。它只显示:&#34;这是测试6&#34;。 但是我需要它在移动时显示所有文字直到:&#34;这是一个测试10&#34;
我没有使用LayoutTransform,因为避免了一些性能问题。 有什么方法可以解决它吗?非常感谢你。
答案 0 :(得分:0)
由我自己修复。 这是因为正在进行动画对象的网格需要更长的宽度宽度父。 将XAML更改为
后修复了问题<Window x:Class="WpfApplication1.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">
<Grid>
<!-- Add a new Grid for the animation object parent, and let it more
long size
This place is the key
-->
<Grid Width="2000">
<Grid x:Name="grdTextblockParent">
<Grid.RenderTransform>
<TranslateTransform x:Name="grdTransform"/>
</Grid.RenderTransform>
<TextBlock x:Name="tb" Text="This is a Test 1This is a Test 2This is a Test 3This is a Test 4This is a Test 5This is a Test 6This is a Test 7This is a Test 8This is a Test 9This is a Test 10" ></TextBlock>
</Grid>
</Grid>
<Button Width="75" Height="50" Content="Start" Click="Button_Click"/>
</Grid>
</Window>