Background property does not point to a dependency

2019-06-15 13:47发布

问题:

I wrote this code and got an exception:

Background property does not point to a dependencyobject in path '(0).(1)'

I saw this problem in other posts in the forum but didn't founded a solution.

<WrapPanel.Style>
  <Style>
    <Style.Triggers>
      <Trigger Property "WrapPanel.Visibility" Value="Visible">                            
        <Trigger.EnterActions>
          <BeginStoryboard HandoffBehavior="Compose">
            <Storyboard RepeatBehavior="Forever" AutoReverse="True">
              <ColorAnimation 
                Storyboard.TargetProperty="(WrapPanel.Background).(SolidColorBrush.Color)"
                Duration="00:00:01" To="Red"/>
            </Storyboard>
          </BeginStoryboard>
        </Trigger.EnterActions>
      </Trigger>
    </Style.Triggers>
  </Style>
</WrapPanel.Style>

Any help with this?

回答1:

You most likely failed to set a value for the initial background brush. You can either do so with a style setter, or else just set a value on the panel directly. The style setter is probably better:

<Setter Property="Background">
    <Setter.Value>
        <SolidColorBrush Color="Blue"/>
    </Setter.Value>
</Setter>

Note that you can also specify the TargetType property on your style, so that you don't have to prefix all property reference with WrapPanel:

<Style TargetType="WrapPanel">


回答2:

You must set the Background property of the WrapPanel! Otherwise the WPF subsystem doesn't recognize it as a SolidColorBrush (could be another brush as well).

<WrapPanel Background="White">
...
</WrapPanel>

is sufficient.