【问题标题】:Error triggering animation based on boolean value基于布尔值触发动画时出错
【发布时间】:2014-10-22 11:47:51
【问题描述】:

当 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);
    }
}

时间问题?

【问题讨论】:

  • TestStoryboard 呢,问题可能就在那里。
  • @KingKing - 故事板确实有效,只是如果我从一开始就将 IsBusy 设置为 true 就不行。我贴出来让你看看。

标签: c# wpf xaml


【解决方案1】:

设置

 <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 之前调用故事板,我相信。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-11
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2017-08-06
    相关资源
    最近更新 更多