基于布尔值触发动画时出错

时间:2014-08-28 14:48:39

标签: c# wpf xaml

当ViewModel上的IsBusy布尔属性为True时,有一个动画的按钮:

<Button x:Name="button" Grid.Row="4"
        Command="{Binding QuitCommand}" 
        Content="{x:Static r:Resources.Close}" 
        RenderTransformOrigin="0.5,0.5">
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsBusy}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource TestStoryboard}"/>
                    </DataTrigger.EnterActions>
                </DataTrigger> **<-- here is line 167 position 27**
            </Style.Triggers>
        </Style>
    </Button.Style>
    <Button.RenderTransform>
        <TransformGroup>
            <ScaleTransform/>
            <SkewTransform/>
            <RotateTransform/>
            <TranslateTransform/>
        </TransformGroup>
    </Button.RenderTransform>
</Button>

故事板:

<Window.Resources>
    <Storyboard x:Key="TestStoryboard">
        <DoubleAnimationUsingKeyFrames 
        Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="25"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="180"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>

如果我将IsBusy属性初始化为false然后稍后启动动画,那么一切正常。

如果我将IsBusy属性初始化为true,则会出现以下错误:

  

'[Unknown]'属性未指向路径中的DependencyObject   '(0)(1)[2]。(2)'。 '设置财产   'System.Windows.FrameworkElement.Style'引发了异常。线   数字'167'和行位置'27'。

视图模型:

public const string IsBusyPropertyName = "IsBusy";

private bool _IsBusy = true;

public bool IsBusy
{
    get
    {
        return _IsBusy;
    }

    set
    {
        if (_IsBusy == value)
        {
            return;
        }

        RaisePropertyChanging(IsBusyPropertyName);
        _IsBusy = value;
        RaisePropertyChanged(IsBusyPropertyName);
    }
}

计时问题?

1 个答案:

答案 0 :(得分:2)

设置

 <Button.RenderTransform>
    <TransformGroup>
        <ScaleTransform/>
        <SkewTransform/>
        <RotateTransform/>
        <TranslateTransform/>
    </TransformGroup>
</Button.RenderTransform>

    <Button.Style>
    <Style TargetType="Button">
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsBusy}" Value="True">
                <DataTrigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource TestStoryboard}"/>
                </DataTrigger.EnterActions>
            </DataTrigger> **<-- here is line 167 position 27**
        </Style.Triggers>
    </Style>
</Button.Style>

如果它已经是真的那么它会在设置RenderTransform之前调用storyboard。我相信。