我在网格中有2列。当我点击一个按钮时,我希望第一列从当前位置左侧动画到0。因此,实际上,它会崩溃,而我只剩下查看单个列。在WPF中,有没有人动画Grid?
回答
退房this视频培训环节,从托德米兰达将告诉您如何动画网格控件的高度。 我认为你可以轻松地让它适用于你的情况。
不应该太难。您需要创建一个EventTrigger,该EventTrigger具有一个以网格为目标的BeginStoryboard并使用DoubleAnimation缩小列宽。 The example here has a similar setup. EventTrigger将继续按钮,DoubleAnimation的StoryBoard.Target将指向您希望缩小的ColumnDefinition。
好吧,这样做效果不好。您不能直接缩小列,但可以将缩小列设置为填充(width =“*”),设置网格和非缩小列的宽度,然后缩小整个网格。这确实有用。下面的示例工作:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowTitle="Opacity Animation Example"
Background="White">
<StackPanel Margin="20">
<Grid Name="MyGrid" Width="200" HorizontalAlignment="Left">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Rectangle HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Grid.Column="0" Fill="Red"/>
<Rectangle HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Grid.Column="1" Fill="Blue"/>
</Grid>
<Button Name="hideButton">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="MyGrid"
Storyboard.TargetProperty="(Grid.Width)"
From="200" To="100"
Duration="0:0:2"
AutoReverse="True" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</StackPanel>
</Page>
你可以做的另一件事是动画内容,并设置网格,以自动调整到内容,它会顺利执行的内容改变大小。
您也可以使用GridLength动画实现此功能,请参阅此处的示例http://marlongrech.wordpress.com/2007/08/20/gridlength-animation/使用此方法可以操纵任何给定的Grid.Column或Grid.Row大小。
为了您的特殊需要,只需在Width =“Auto”中放置第一列,然后在第二列中添加*,为第一列中的内容设置动画效果。
您还可以使用从凯文的袋邻花样显示控制,http://j832.com/bagotricks/
我已经采取了托德米兰达的C#源代码并对其进行了修改,以演示如何动画化DataGrid列宽度的缩小&的扩展。
这里的源代码
http://www.pocketpctoolkit.com/WPF/DataGridColumnWidthAnimation.zip
基本上,你点击一个复选框,并取其DataGrid中列有他们的“MinWidth”值设置为0将被缩小到零宽度。再次单击复选框,这些列将回到原始宽度。
此WPF代码还演示了如何在代码后面创建动画/故事板。
你需要创建一个GridLengthAnimation类(从代码中:http://windowsclient.net/learn/video.aspx?v=70654)
public class GridLengthAnimation : AnimationTimeline
{
public GridLengthAnimation()
{
// no-op
}
public GridLength From
{
get { return (GridLength)GetValue(FromProperty); }
set { SetValue(FromProperty, value); }
}
public static readonly DependencyProperty FromProperty =
DependencyProperty.Register("From", typeof(GridLength), typeof(GridLengthAnimation));
public GridLength To
{
get { return (GridLength)GetValue(ToProperty); }
set { SetValue(ToProperty, value); }
}
public static readonly DependencyProperty ToProperty =
DependencyProperty.Register("To", typeof(GridLength), typeof(GridLengthAnimation));
public override Type TargetPropertyType
{
get { return typeof(GridLength); }
}
protected override Freezable CreateInstanceCore()
{
return new GridLengthAnimation();
}
public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock)
{
double fromValue = this.From.Value;
double toValue = this.To.Value;
if (fromValue > toValue)
{
return new GridLength((1 - animationClock.CurrentProgress.Value) * (fromValue - toValue) + toValue, this.To.IsStar ? GridUnitType.Star : GridUnitType.Pixel);
}
else
{
return new GridLength((animationClock.CurrentProgress.Value) * (toValue - fromValue) + fromValue, this.To.IsStar ? GridUnitType.Star : GridUnitType.Pixel);
}
}
}
而对于RowDefinition/ColumnDefinition故事板。
<Window.Resources>
<Storyboard x:Key="ColumnAnimation">
<Animations:GridLengthAnimation
BeginTime="0:0:0"
Duration="0:0:0.1"
From="0*"
Storyboard.TargetName="ColumnToAnimate"
Storyboard.TargetProperty="Width"
To="10*" />
</Storyboard>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition x:Name="ColumnToAnimate" Width="0*" />
</Grid.ColumnDefinitions>
</Grid>
- 1. XAML没有WPF - 动画
- 2. ng-grid headerRowTemplate - 有没有人使用过?
- 3. 有没有人用过WPF DataGrid的ADOMD.NET?
- 4. ViewFlipper没有动画?
- 5. Flexslider没有动画
- 6. presentModalViewController没有动画
- 7. CAShapeLayer没有动画
- 8. 有没有办法在WPF中为图像的边缘添加动画?
- 9. Dialog在Android中没有动画
- 10. 在没有ButtonChrome的WPF按钮按动画?
- 11. 动画gif没有提交动画
- 12. 动画layoutIfNeeded UITableView没有动画UITableViewCells
- 13. 核心动画:光栅没有动画
- 14. 链式变换动画没有动画
- 15. Zepto动画回调没有动画
- 16. iOS:UIView动画,没有动画发生
- 17. WPF - 没有容器的DataTemplate,作为Grid中行的项目?
- 18. WPF动画:检测是否有任何动画正在处理?
- 19. ListView中的动画ToggleButton在切换时没有动画闪烁?
- 20. 无法在cocos2d中加载plist动画“没有找到动画”
- 21. 动画活动没有工作在android
- 22. 有没有人有经验在WorkFront/AtTask中启动“注释”?
- 23. 动画GIF不在WPF中动画
- 24. splitViewController showDetailViewController没有动画?
- 25. 动画没有滞后?
- 26. 没有动画的DialogFragment
- 27. MaxImage:没有动画效果
- 28. jquery没有动画“隐藏”
- 29. 没有动画的ListActivity
- 30. 没有jQuery的动画JavaScript?
尼斯视频,谢谢:) – 2012-04-12 05:13:19