增大/缩小 WPF 动画
在 WPF 中,当用户在 TextBox
中获得焦点时,我想要一些动画,使 TextBox
变为多行并使其 Width
更大(当他打字时)并且当焦点丢失时,TextBox
会恢复到其原始大小。
尺寸未知。
此外,最终,TextBox
需要位于 WPF DataGrid
中。
我以前从未制作过动画,希望得到一些帮助来帮助我入门。谢谢。
编辑:我已经成功地制作了动画,同时具有一些固定的宽度值(使其成为多行不起作用,但它并不那么重要)。所以我现在的问题是,如果未知,我怎样才能恢复到原来的大小。我可以在 Width
属性上使用乘数吗?
到目前为止,这是我的代码:
<Window.Resources>
<Storyboard x:Key="GrowStoryboard">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textBox" Storyboard.TargetProperty="(FrameworkElement.Width)">
<SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="400" KeySpline="0.54,0.27,0.38,0.69"/>
</DoubleAnimationUsingKeyFrames>
<Int32Animation Duration="0:0:0.4" From="1" To="3" Storyboard.TargetName="textBox" Storyboard.TargetProperty="(TextBox.MinLines)">
</Int32Animation>
</Storyboard>
<Storyboard x:Key="ShrinkStoryboard">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textBox" Storyboard.TargetProperty="(FrameworkElement.Width)">
<SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="200" KeySpline="0.54,0.27,0.38,0.69"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="FocusManager.GotFocus" SourceName="textBox">
<BeginStoryboard x:Name="GrowStoryboard_BeginStoryboard" Storyboard="{StaticResource GrowStoryboard}"/>
</EventTrigger>
<EventTrigger RoutedEvent="FocusManager.LostFocus" SourceName="textBox">
<BeginStoryboard x:Name="ShrinkStoryboard_BeginStoryboard" Storyboard="{StaticResource ShrinkStoryboard}"/>
</EventTrigger>
</Window.Triggers>
<StackPanel>
<TextBox x:Name="textBox" Width="200" Height="20" Text="TextBox" />
<TextBox x:Name="textBox1" Width="200" Height="20" Text="TextBox" />
</StackPanel>
In WPF, when a user gets focus in a TextBox
, I would like some animation that would make the TextBox
becomes multiline and make its Width
bigger (while he is typing) and when the focus is lost, that the TextBox
goes back to its original size.
The size is unknown.
Also, ultimately, that TextBox
needs to be within a WPF DataGrid
.
I have never done animation before, and would like some help to get me started. Thanks.
EDIT: I have succeeded in doing the animation while having the some fixed width values (making it multiline did not work, but it is not that important). So my question now is how can I go back to my original size if that is unknown. Is there multiplier I could use on the Width
property?
Here is my code so far:
<Window.Resources>
<Storyboard x:Key="GrowStoryboard">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textBox" Storyboard.TargetProperty="(FrameworkElement.Width)">
<SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="400" KeySpline="0.54,0.27,0.38,0.69"/>
</DoubleAnimationUsingKeyFrames>
<Int32Animation Duration="0:0:0.4" From="1" To="3" Storyboard.TargetName="textBox" Storyboard.TargetProperty="(TextBox.MinLines)">
</Int32Animation>
</Storyboard>
<Storyboard x:Key="ShrinkStoryboard">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textBox" Storyboard.TargetProperty="(FrameworkElement.Width)">
<SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="200" KeySpline="0.54,0.27,0.38,0.69"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="FocusManager.GotFocus" SourceName="textBox">
<BeginStoryboard x:Name="GrowStoryboard_BeginStoryboard" Storyboard="{StaticResource GrowStoryboard}"/>
</EventTrigger>
<EventTrigger RoutedEvent="FocusManager.LostFocus" SourceName="textBox">
<BeginStoryboard x:Name="ShrinkStoryboard_BeginStoryboard" Storyboard="{StaticResource ShrinkStoryboard}"/>
</EventTrigger>
</Window.Triggers>
<StackPanel>
<TextBox x:Name="textBox" Width="200" Height="20" Text="TextBox" />
<TextBox x:Name="textBox1" Width="200" Height="20" Text="TextBox" />
</StackPanel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然 XAML 中没有可以使用的乘数,但您可以创建一个 IValueConverter 类来完成此操作。例如:
然后您可以在 XAML 中使用它来使文本框的宽度按如下因子增大和缩小...
While there is no multiplier you could use in XAML, you could create a IValueConverter class to accomplish this. For example:
Then you can use this in the XAML to make the Textbox's width grow and shrink by a factor like so...