开发者

WPF animation throws Exception "'Background' property does not point to a DependencyObject in path '(0).(1)'."

开发者 https://www.devze.com 2023-01-15 17:56 出处:网络
I have got some WPF source: <Window x:Class=\"WpfApplication2.MainWindow\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"

I have got some WPF source:

<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>
                    &l开发者_如何转开发t;/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>

I fill the list with:

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_;
}

The problem is, that i got an exception 'Background' property does not point to a DependencyObject in path '(0).(1)'. I dont know why :-( Does andybody know?

Thanks regards Michael


This exception is thrown when there is no Background property set on the TextBlock when the animation commences. The one-liner solution is to ensure you set an initial background in the TextBlock to begin with, and doing this via {DynamicResource} does not guarantee that for you.


Is there a specific reason why you use DynamicResource instead of StaticResource? As far as I know you should only use DynamicResource when the Style is updated dynamically.

If I change the DynamicResource in StaticResource, the code is working for me.

Edit: Here is the code that worked for me:

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

And my 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_;
    }
}


Here's a working example that could help you:

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" />

If you are starting the animation from C# ...

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

精彩评论

暂无评论...
验证码 换一张
取 消