通过Storyboard调整Grid.Column中的网格大小?

时间:2014-01-01 23:21:53

标签: c# xaml winrt-xaml

我有一个Grid定义了两个ColumnDefinition s:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="column0" Width="*"/>
        <ColumnDefinition x:Name="column1" Width="Auto"/>
    </Grid.ColumnDefinitions>

    <Grid Grid.Column="0" x:Name="content0">
    </Grid>

    <Grid Grid.Column="1" x:Name="content1">
        <Button Content="Press me!" />
    </Grid>
</Grid>

现在我想使用Storyboard创建折叠动画 因此我创造了这个:

<Storyboard x:Key="myStoryboard">
    <DoubleAnimation
        Storyboard.TargetName="content1"
        Storyboard.TargetProperty="Width"
        From="??" To="0" Duration="0:0:1" EnableDependentAnimation="True" />
</Storyboard>

我的问题是From属性 如果我输入一个像100这样的特定值,一切正常,但我想像From={Binding ElementName=content1, Path=ActualWidth}这样做不行的东西。
我试图创建一个IValueConverter但这些不适用于From 此外,如果我在代码隐藏中设置content1.Width = 100,它也会起作用。

我错过了什么或者我使用了错误的动画类型吗?

编辑:我尝试在单独的项目中做类似的事情以进行测试 我检查它确实有效,但{Binding ElementName=content1, Path=ActualWidth的返回值似乎始终为0.使用{Binding ElementName=content1, Path=Width时也是如此。不幸的是,我不知道为什么值总是为0。

Edit2:其实我不太确定我的第一次编辑是否正确。

2 个答案:

答案 0 :(得分:3)

你可以在代码中而不是Xaml中执行此操作。根据你的例子做这样的事情

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MoveGrid();
    }

    private void MoveGrid()
    {
        var sb = new Storyboard();
        var animation = new DoubleAnimation()
        {
            To = 0,
            From = content1.ActualWidth,
            EnableDependentAnimation = true
        };
        Storyboard.SetTargetProperty(animation, "Width");
        Storyboard.SetTarget(animation, content1);
        sb.Children.Add(animation);

        sb.Begin();
    }

答案 1 :(得分:1)

我认为这就是你所需要的: http://www.codeproject.com/Articles/18379/WPF-Tutorial-Part-2-Writing-a-custom-animation-cla

简而言之,WidthGridLength,因此您需要一个自定义的GridLengthAnimation课程。