CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets My Favorites Favorites Web Code Search Snippets Search
Sign inor Register
Language: C#

Animating a ListBoxItem's Size in Silverlight

286 Views   
private void MyListBox_SelectionChanged(object sender, 
    SelectionChangedEventArgs e)
{
    // get the ListBoxItem that was pressed
    var _UIElement = MyListBox.ItemContainerGenerator
        .ContainerFromItem(MyListBox.SelectedItem) as UIElement;
 
    // resize the ListBoxItem
    Resize(_UIElement, () => { /* TODO: nav away */ }, 1, .25, .1);
}
 
private static void Resize(UIElement item, Action callback,
    double from, double to, double duration = .1)
{
    // animate size
    var _Transform = new ScaleTransform
    {
        CenterX = item.RenderSize.Width * .5,
        CenterY = item.RenderSize.Height * .5,
        ScaleX = from,
        ScaleY = from
    };
    item.RenderTransform = _Transform;
    var _Storyboard = new Storyboard
    {
        Duration = TimeSpan.FromSeconds(duration),
        AutoReverse = false
    };
 
    // Y
    var _AnimationY = new DoubleAnimation { To = to };
    _Storyboard.Children.Add(_AnimationY);
    Storyboard.SetTarget(_AnimationY, _Transform);
    Storyboard.SetTargetProperty(_AnimationY,
        new PropertyPath(ScaleTransform.ScaleYProperty));
 
    // X
    var _AnimationX = new DoubleAnimation { To = to };
    _Storyboard.Children.Add(_AnimationX);
    Storyboard.SetTarget(_AnimationX, _Transform);
    Storyboard.SetTargetProperty(_AnimationX,
        new PropertyPath(ScaleTransform.ScaleXProperty));
    _Storyboard.Completed += (s, arg) =>
    {
        if (callback != null)
            callback();
    };
 
    // Go!
    _Storyboard.Begin();
}
by Jerry Nixon
  January 03, 2012 @ 3:42pm
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies