c# – WPF动画抛出异常“’Background’属性不指向路径'(0)中的DependencyObject.(1)’.”

前端之家收集整理的这篇文章主要介绍了c# – WPF动画抛出异常“’Background’属性不指向路径'(0)中的DependencyObject.(1)’.”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一些 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.
我不知道为什么:-(你和别人都知道吗?

谢谢
问候
迈克尔

解决方法

您使用DynamicResource而不是StaticResource的具体原因是什么?据我所知,你应该只在动态更新Style时使用DynamicResource.

如果我在StaticResource中更改DynamicResource,代码对我有用.

编辑:这是适合我的代码

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

猜你在找的C#相关文章