我有一个 Grid 定义了两个 ColumnDefinition:
<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:其实我不太确定我的第一次编辑是否正确。
请您参考如下方法:
您可以在代码中而不是在 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();
}
