29

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>

3 Answers 3

45

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.

6
  • I added the storyboard code. I do bind the 'To' property since I don't want to hardcode the width.
    – jwarzech
    Nov 3, 2009 at 20:43
  • 1
    That's your problem then, I will explain.
    – Drew Marsh
    Nov 3, 2009 at 20:47
  • 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
    Nov 3, 2009 at 21:26
  • 3
    This happened to me not due Binding, but using Dynamicresources! Thanks for the hint!
    – dba
    Dec 2, 2015 at 8:52
  • 3
    This happened to me too with DynamicResources. How can I use dynamic resources in this case? I need to reference a colour which I have in a Resource Dictionary Nov 11, 2017 at 12:32
11

There is a technique that you can use to get around the Freezable issue that allows you to use a binding for the "To" value of your animation (rather than hard-coding a value there). Its pretty straightforward and I've outlined it here.

1
  • Thank you for posting this. However, since this technique uses the Tag property, it must be added that it works to animate one property, like in the OP example, the Width. If you plan to use this approach for Width and Height, for a more complex animation, then it does not work, unless you animate a square. Dec 14, 2022 at 12:00
3

Old question but might be useful for other people. Sometimes creating the Storyboard in the code-behind can be simpler: https://stackoverflow.com/a/10848781/779521

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.