WPF动画抛出异常“'背景'属性不指向路径中的DependencyObject'(0)。(1)'。”

时间:2010-09-09 15:50:11

标签: c# wpf

我有一些WPF来源:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <SolidColorBrush x:Key="RedBrush" Color="Red"/>
    <SolidColorBrush x:Key="GreenBrush" Color="Green"/>
    <SolidColorBrush x:Key="TransparentBrush" Color="Transparent"/>

    <DataTemplate x:Key="MyItemTemplate">
        <Grid Margin="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <TextBlock Background="{DynamicResource TransparentBrush}">
                <TextBlock.Style>
                    <Style>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding State}" Value="Stat1">
                                <Setter Property="TextBlock.Background" Value="{DynamicResource RedBrush}" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding State}" Value="Stat2">                                    
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard RepeatBehavior="Forever">
                                            <ColorAnimation
                                                Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
                                                Duration="00:00:01"
                                                From="Yellow" To="Red"/>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
                <TextBlock.Text>
                    <Binding Path="Name" />
                </TextBlock.Text>
            </TextBlock>
        </Grid>
    </DataTemplate>
</Window.Resources>

<ListBox x:Name="SomeList"
         HorizontalContentAlignment="Stretch"
         VerticalContentAlignment="Top"
         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         ScrollViewer.VerticalScrollBarVisibility="Visible"              
         ItemTemplate="{StaticResource MyItemTemplate}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="3" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

我在列表中填写:

public MainWindow()
{
    InitializeComponent();

    myList_ = new List<Foo>();

    myList_.Add(new Foo() { State = "Stat1", Name = "Name 1" });
    myList_.Add(new Foo() { State = "Stat2", Name = "Name 2" });

    SomeList.ItemsSource = myList_;
}

问题是,我有一个例外 'Background'属性不指向路径'(0)。(1)'中的DependencyObject。 我不知道为什么:-(有人知道吗?

由于 问候 迈克尔

3 个答案:

答案 0 :(得分:8)

当动画开始时,TextBlock上没有设置Background属性时抛出此异常。单线解决方案是确保您在TextBlock中设置初始背景,并且通过{DynamicResource}执行此操作并不能保证为您服务。

答案 1 :(得分:5)

您使用DynamicResource代替StaticResource的具体原因是什么?据我所知,你应该只在动态更新样式时使用DynamicResource

如果我更改了DynamicResource中的StaticResource,则代码对我有效。

编辑:以下代码对我有用:

<Window x:Class="BackgroundTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <SolidColorBrush x:Key="RedBrush" Color="Red"/>
    <SolidColorBrush x:Key="GreenBrush" Color="Green"/>
    <SolidColorBrush x:Key="TransparentBrush" Color="Transparent"/>

    <DataTemplate x:Key="MyItemTemplate">
        <Grid Margin="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <TextBlock Background="{StaticResource TransparentBrush}">
            <TextBlock.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding State}" Value="Stat1">
                            <Setter Property="TextBlock.Background" Value="{StaticResource RedBrush}" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding State}" Value="Stat2">                                    
                            <DataTrigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard RepeatBehavior="Forever">
                                        <ColorAnimation
                                            Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
                                            Duration="00:00:01"
                                            From="Yellow" To="Red"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
            <TextBlock.Text>
                <Binding Path="Name" />
            </TextBlock.Text>
            </TextBlock>
        </Grid>
    </DataTemplate>
</Window.Resources>

<ListBox x:Name="SomeList"
     HorizontalContentAlignment="Stretch"
     VerticalContentAlignment="Top"
     ScrollViewer.HorizontalScrollBarVisibility="Disabled"
     ScrollViewer.VerticalScrollBarVisibility="Visible"              
     ItemTemplate="{StaticResource MyItemTemplate}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="3" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

我的MainWindow.xaml.cs

public partial class MainWindow : Window
{
    IList<Foo> myList_;

    public MainWindow()
    {
        InitializeComponent();

        myList_ = new List<Foo>();

        myList_.Add(new Foo() { State = "Stat1", Name = "Name 1" });
        myList_.Add(new Foo() { State = "Stat2", Name = "Name 2" });

        SomeList.ItemsSource = myList_;
    }
}

答案 2 :(得分:0)

这是一个可以帮助您的工作示例:

XAML:

    <Storyboard x:Key="UpdateDetectedStoryboard">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
                                      Storyboard.TargetName="TextBlockUpdatesDetected">
            <EasingColorKeyFrame KeyTime="0:0:1"
                                 Value="#f07FA7BA" />
        </ColorAnimationUsingKeyFrames>
    </Storyboard>



                <TextBlock Name="TextBlockUpdatesDetected"                               
                          Text="{x:Static res:Resource1.UpdatesDetected}"
                          FontSize="10"
                          Background="#FFD42121" />

如果您是从C#开始制作动画......

                Storyboard animation = this.Resources["UpdateDetectedStoryboard"] as Storyboard;
                animation.Begin();