0

StackPanel に新しいアイテムを追加するときに素敵なアニメーションを表示するために、このイベントを StackPanel に追加しました。

 expandableStack.SizeChanged += (s, e) =>
        {
            DoubleAnimation expand = new DoubleAnimation();
            expand.Duration = TimeSpan.FromMilliseconds(250);
            expand.From = e.PreviousSize.Height;
            expand.To = e.NewSize.Height;
            expandableStack.BeginAnimation(HeightProperty, expand);
        };

新しいサイズが以前のサイズよりも大きい場合はうまく機能しますが、小さい場合 (アイテムを削除したとき)、StackPanel はサイズを変更しないため、イベント SizeChanged は発生しません。

StackPanel をコンテンツに合わせて調整するにはどうすればよいですか? または、StackPanel 内のアイテムのサイズを取得するにはどうすればよいですか。すべての Size/Height プロパティを試しましたが、どれもそれを表していません。

            MessageBox.Show("Height: " + expandableStack.Height.ToString());
            MessageBox.Show("ActualHeight: " + expandableStack.ActualHeight.ToString());
            MessageBox.Show("Render size: " + expandableStack.RenderSize.Height.ToString());
            MessageBox.Show("ViewportHeight size: " + expandableStack.ViewportHeight.ToString());
            MessageBox.Show("DesiredSize.Height size: " + expandableStack.DesiredSize.Height.ToString());
            MessageBox.Show("ExtentHeight size: " + expandableStack.ExtentHeight.ToString());
            MessageBox.Show("VerticalOffset size: " + expandableStack.VerticalOffset.ToString());
4

1 に答える 1

1

あなたの状況では、データ ソースとして、ObservableCollectionたとえば、、、ItemsControlなどを使用するコントロールを使用する必要があると思いますListBox。これはevent CollectionChangedであるため、コレクションに対して実行された行為の列挙が含まれています [ MSDN ]:

Member name   Description
------------  ------------
Add           One or more items were added to the collection.
Move          One or more items were moved within the collection.
Remove        One or more items were removed from the collection.
Replace       One or more items were replaced in the collection.
Reset         The content of the collection changed dramatically.

このイベントは次のように実装されます。

// Set the ItemsSource
SampleListBox.ItemsSource = SomeListBoxCollection;

// Set handler on the collection
SomeListBoxCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(SomeListBoxCollection_CollectionChanged);

private void SomeListBoxCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.Action == NotifyCollectionChangedAction.Add)
    {
        // Some actions, in our case - start the animation
    }
}

アニメーションの要素を追加するより詳細な例(ListBox)、私の答えを参照してください:

WPF DataBound ListBox追加時にアニメーション化するがスクロールしない

ListBoxelement は、任意のタイプの要素にすることができますControl

于 2013-07-15T06:42:56.703 に答える