在 WPF 中,有人动画网格吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/197855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In WPF, has anybody animated a Grid?
提问by ScottG
I have 2 columns in a Grid. When I click a button, I want the first column to animate to the left from it's current position to 0. So, in effect, it collapses and I'm left with just viewing a single column.
我在网格中有 2 列。当我单击一个按钮时,我希望第一列从当前位置向左移动到 0。因此,实际上,它会折叠起来,而我只剩下查看单个列。
采纳答案by Nikos Tsokos
回答by ScottG
Shouldn't be too hard. You'd need to create an EventTrigger that has a BeginStoryboard that targets the grid and uses a DoubleAnimation to shrink the column width. The example here has a similar setup.The EventTrigger would go on the button and the DoubleAnimation's StoryBoard.Targetwould point to the ColumnDefinition you wish to shrink.
应该不会太难。您需要创建一个 EventTrigger,它具有一个以网格为目标的 BeginStoryboard,并使用 DoubleAnimation 来缩小列宽。 此处的示例具有类似的设置。EventTrigger 将放在按钮上,DoubleAnimation 的StoryBoard.Target将指向您希望缩小的 ColumnDefinition。
Okay, so that doesn't work so well. You can't shrink the column directly, but you CAN set the shrinking column to fill (width="*"), set the width of the Grid and the non-shrinking column, and then shrink the entire grid. This does work. The below example works:
好吧,所以效果不太好。不能直接收缩列,但可以设置收缩列填充(width="*"),设置Grid和非收缩列的宽度,然后收缩整个网格。这确实有效。下面的例子有效:
<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>
回答by Aaron Hoffman
You need to Create a GridLengthAnimation class (code from: http://windowsclient.net/learn/video.aspx?v=70654)
您需要创建一个 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);
}
}
}
And a Storyboard for the RowDefinition/ColumnDefinition.
以及 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>
回答by Nir
You can also use the Reveal control from Kevin's bag-o-tricks, http://j832.com/bagotricks/
您还可以使用 Kevin 的 bag-o-tricks 中的 Reveal 控件,http://j832.com/bagotricks/
回答by Nidonocu
Another thing you can do is animate the contents and set the Grid to autosize to content which it will do smoothly as the contents changes size.
您可以做的另一件事是为内容设置动画并将网格设置为自动调整内容,随着内容更改大小,它将顺利完成。
回答by Jobi Joy
You can also achieve this with GridLength animation , see an example here http://marlongrech.wordpress.com/2007/08/20/gridlength-animation/Using this approach you can manipulate any given Grid.Column or Grid.Row size.
您也可以使用 GridLength 动画来实现这一点,请参阅此处的示例http://marlongrech.wordpress.com/2007/08/20/gridlength-animation/使用此方法您可以操作任何给定的 Grid.Column 或 Grid.Row 大小。
For your special need just put first column with Width="Auto" and second with *, animate the with of the content inside the first column- that will do the trick.
对于您的特殊需要,只需将第一列设置为 Width="Auto",第二列设置为 *,为第一列内的内容设置动画 - 这样就可以了。
回答by Mike Gledhill
I have taken Todd Miranda's C# source code and modified it, to demonstrate how to animate DataGrid Column widths shrinking & expanding.
我采用了 Todd Miranda 的 C# 源代码并对其进行了修改,以演示如何为 DataGrid 列宽缩小和扩展设置动画。
Here's the source code...
这是源代码...
http://www.pocketpctoolkit.com/WPF/DataGridColumnWidthAnimation.zip
http://www.pocketpctoolkit.com/WPF/DataGridColumnWidthAnimation.zip
Basically, you click on a CheckBox, and whichever DataGrid columns have their "MinWidth" value set to 0 will be shrunk to zero-width. Click the CheckBox again, the columns will animate back to their original widths.
基本上,您单击复选框,并且将“MinWidth”值设置为 0 的任何 DataGrid 列都将缩小为零宽度。再次单击 CheckBox,列将动画恢复为其原始宽度。
This WPF code also demonstrates how to create animations / storyboards in code behind.
此 WPF 代码还演示了如何在后面的代码中创建动画/故事板。