题
我在网格2列。当我点击一个按钮,我想第一列以动画向左侧从它的当前位置为0。因此,实际上,它缩短了,我只剩下观看一列。
解决方案
请查看这的视频培训环节,从托德米兰达将告诉您如何动画网格控件的高度。 我想,你可以很容易使你的情况下工作。
其他提示
不应该太硬。你需要创建具有针对电网和使用DoubleAnimation是缩小列宽一BeginStoryboard一个EventTrigger。 的例子在这里有一个类似的设置。一>的EventTrigger会去上的按钮和DoubleAnimation是的的 StoryBoard.Target 将指向要收缩ColumnDefinition。
好了,所以不那么有效。你不能直接缩小列,但你可以设置萎缩栏,须填写(宽度=“*”),设置网格和非收缩列的宽度,然后再缩小整个电网。这并不工作。下面的例子如下:
<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>
您需要创建一个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>
可以做的另一件事是动画内容,并设置格来自动调整到内容它将顺利做作为内容的变化的大小。
您也可以使用来自凯文的袋邻花样显示控制, http://j832.com/bagotricks /
您也可以GridLength动画实现这一点,在这里看到的例子 HTTP ://marlongrech.wordpress.com/2007/08/20/gridlength-animation/ 使用这种方法可以操作任何给定的Grid.Column或Grid.Row大小。
有关您的特殊需要的只是把第一列具有宽度=“自动”和第二带*号,与第一栏柱里面的内容,会做的伎俩的动画。
我已采取托德米兰达的C#源代码和修改它,以证明如何动画数据网格列宽缩小&扩大。
下面的源代码...
http://www.pocketpctoolkit.com/WPF/DataGridColumnWidthAnimation.zip
基本上,你点击一个CheckBox和取其数据网格列具有其“MinWidth”值设置为0将被缩小到零宽度。再次单击该复选框,列将动画回到原来的宽度。
此WPF代码还演示了如何在代码创建动画/故事板的后面。