简体   繁体   中英

WP7: LayoutTransform a ListBoxItem

Here's my XAML:

<ListBox x:Name="MyListBox" FontSize="40"
            SelectionChanged="MyListBox_SelectionChanged">
</ListBox>            

In this code-behind (below), I am attempting to animate a delete action. When the item is selected I delete it. I visually animate it with a ScaleTransform. In WPF I would use a LayoutTransform, but since I only have RenderTransform in WP/SL, I am using RenderTransform - and as a result the surrounding layout is not responding to the change in size. The record is still correctly deleted, but the visual effect is diminished.

Is there a way to do this in WP? Is there a way to resize a ListBoxItem so that the surrounding content responds?

ObservableCollection<string> m_Data;

public MainPage()
{
    InitializeComponent();
    m_Data = new ObservableCollection<string>   
        { "One", "Two", "Three", "Four" };
    MyListBox.ItemsSource = m_Data;
}

private void MyListBox_SelectionChanged(object sender,
    SelectionChangedEventArgs e)
{
    // fetch ListBoxItem
    if (e.AddedItems.Count == 0)
        return;
    var _Data = e.AddedItems[0] as string;
    var _Item = MyListBox.ItemContainerGenerator
        .ContainerFromItem(_Data) as ListBoxItem;

    // setup to resize using scale transform
    var _Scale = new ScaleTransform
    {
        CenterX = _Item.RenderSize.Width / 2,
        CenterY = _Item.RenderSize.Height / 2,
        ScaleX = .99,
        ScaleY = .99
    };
    _Item.RenderTransform = _Scale;

    // setup storyboard
    var _Story = new Storyboard();
    _Story.Completed += (s, e1) =>
    {
        // remove data from collection
        m_Data.Remove(_Data);
    };

    // animate scale X
    var _AnimationX = new DoubleAnimation
    {
        To = .01,
        Duration = TimeSpan.FromSeconds(2),
    };
    _Story.Children.Add(_AnimationX);
    Storyboard.SetTarget(_AnimationX, _Scale);
    Storyboard.SetTargetProperty(_AnimationX,
        new PropertyPath(ScaleTransform.ScaleXProperty));

    // animate scale Y
    var _AnimationY = new DoubleAnimation
    {
        To = .01,
        Duration = TimeSpan.FromSeconds(2),
    };
    _Story.Children.Add(_AnimationY);
    Storyboard.SetTarget(_AnimationY, _Scale);
    Storyboard.SetTargetProperty(_AnimationY,
        new PropertyPath(ScaleTransform.ScaleYProperty));

    _Story.Begin();
}

You can also use LayoutTransform on Windows Phone so I would just use that. .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM