WPFでは、誰かがグリッドをアニメーション化しましたか?
質問
グリッドに2つの列があります。ボタンをクリックすると、最初の列が現在の位置から0まで左にアニメーション化されるようにしたいので、実際には、折りたたまれて、1つの列だけが表示されたままになります。
解決
Todd Mirandaのこちらのビデオトレーニングリンクをご覧ください。グリッドコントロールの高さをアニメーション化する方法。 あなたのケースで簡単に機能させることができると思います。
他のヒント
難しくありません。グリッドをターゲットとし、DoubleAnimationを使用して列幅を縮小するBeginStoryboardを持つEventTriggerを作成する必要があります。 この例には同様の設定があります。 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>
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つできることは、コンテンツをアニメーション化し、コンテンツのサイズが変更されるとスムーズに行われるコンテンツに自動サイズ変更するようにグリッドを設定することです。
Kevinのbag-o-tricks、 http://j832.com/bagotricksからRevealコントロールを使用することもできます。 /
GridLengthアニメーションでこれを実現することもできます。ここの例を参照してください http ://marlongrech.wordpress.com/2007/08/20/gridlength-animation/ このアプローチを使用すると、指定されたGrid.ColumnまたはGrid.Rowのサイズを操作できます。
特別な必要性のために、Width =&quot; Auto&quot;で最初の列を置くだけです。 2番目の*で、最初の列内のコンテンツのwithをアニメーション化します。これにより、トリックが実行されます。
トッドミランダのC#ソースコードを取得して変更し、DataGridの列幅を縮小して&amp;をアニメーション化する方法を示しました。エキスパンド。
ソースコードは次のとおりです...
http://www.pocketpctoolkit.com/WPF/DataGridColumnWidthAnimation.zip
基本的に、CheckBoxをクリックします。DataGridの列に「MinWidth」がある場合は、 0に設定された値は、ゼロ幅に縮小されます。チェックボックスをもう一度クリックすると、列が元の幅にアニメーション化されます。
このWPFコードは、コードビハインドでアニメーション/ストーリーボードを作成する方法も示しています。