簡體   English   中英

通過情節提要在Grid.Column中調整網格大小?

[英]Resizing Grid in Grid.Column via Storyboard?

我有一個定義兩個ColumnDefinitionGrid

<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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM