views:

645

answers:

1

I currently have a listbox that has its selected item bound to a property on my ViewModel. Whenever the selected item isn't null I want to perform an animation on it. However I keep getting the following error "Cannot freeze this Storyboard timeline tree for use across threads" and from research sort of understand why this is happening. However I am unsure of what approach I need to take to get the behavior I want.

<Storyboard x:Key="ShowItemEdit">
    <DoubleAnimation
        Storyboard.TargetName="lstItemList"
        Storyboard.TargetProperty="ListBox.Width"
        To="{Binding ActualWidth, ElementName=UserControl}"
        Duration="0:0:0.40" />
    ...
</Storyboard>

<Style x:Key="ListStyle">
    <Style.Triggers>
        <DataTrigger Binding="{Binding SelectedItem, Converter={StaticResource IsNullConverter}}" Value="False">
            <DataTrigger.EnterActions>
            <BeginStoryboard Storyboard="{StaticResource ShowItemEdit}" />
        </DataTrigger.EnterActions>
        </DataTrigger>
     </Style.Triggers>
</Style>

<ListBox x:Name="lstItemList" Style={StaticResource ListStyle}" SelectedItem="{Binding SelectedItem}">
    ...
</ListBox>
+1  A: 

Can you post your Storyboard? It sounds like you have some kind of Binding in the Storyboard definition.


Ok so, as I suspected, it's because you're using a Binding in your Storyboard. You can't do this because WPF attempts to freeze all the resources leveraged by a template for efficiency and when you use a Binding on a Freezable, in this case the Storyboard, it prevents it from being able to be frozen.

Drew Marsh
I added the storyboard code. I do bind the 'To' property since I don't want to hardcode the width.
jwarzech
That's your problem then, I will explain.
Drew Marsh
Thanks for the explanation! Any suggestion on how I can achieve this storyboard (with binding) without having to connect the datatrigger to the control?
jwarzech
Ehhh, based on what I can guess you're trying to do with the animation how about instead of setting a Width on the the ListBox you set it's HorizontalAlignment to Stretch and then give it a Margin and this storyboard animates the Margin to 0 instead of the Width to the ActualWidth of the UserControl?
Drew Marsh