[英]Resizing Grid in Grid.Column via Storyboard?
我有一個定義兩個ColumnDefinition
的Grid
:
<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();
}
我認為這是您需要的: http : //www.codeproject.com/Articles/18379/WPF-Tutorial-Part-2-Writing-a-custom-animation-cla
簡而言之, Width
是一個GridLength
因此您需要一個自定義GridLengthAnimation
類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.