28

我目前有一个列表框,它的选定项绑定到我的 ViewModel 上的属性。每当所选项目不为空时,我都想对其执行动画。但是,我不断收到以下错误“无法冻结此 Storyboard 时间线树以供跨线程使用”,并且从研究中了解到为什么会发生这种情况。但是我不确定我需要采取什么方法来获得我想要的行为。

<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>
4

3 回答 3

43

你可以发布你的故事板吗?听起来您在故事板定义中有某种绑定。


好的,正如我所怀疑的那样,这是因为您BindingStoryboard. 您不能这样做,因为 WPF 尝试冻结模板利用的所有资源以提高效率,并且当您在 a 上使用 Binding 时Freezable,在这种情况下Storyboard,它会阻止它被冻结。

于 2009-11-03T20:00:46.700 回答
11

您可以使用一种技术来解决 Freezable 问题,该技术允许您对动画的“To”值使用绑定(而不是在那里硬编码一个值)。它非常简单,我在这里概述了它。

于 2013-01-30T16:34:30.600 回答
3

老问题,但可能对其他人有用。有时在代码隐藏中创建故事板可能更简单:https ://stackoverflow.com/a/10848781/779521

于 2012-06-01T10:44:18.993 回答