Background属性不指向路径'(0)中的dependencyobject.(1)'

May*_*aya 12 c# wpf animation triggers styles

我写了这段代码并得到了一个例外:

Background属性不指向路径'(0)中的dependencyobject.(1)'

我在论坛的其他帖子中看到了这个问题,但没有找到解决方案.

<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>
Run Code Online (Sandbox Code Playgroud)

对此有何帮助?

dle*_*lev 24

您很可能无法为初始背景画笔设置值.您可以使用样式设置器执行此操作,也可以直接在面板上设置值.样式设定器可能更好:

<Setter Property="Background">
    <Setter.Value>
        <SolidColorBrush Color="Blue"/>
    </Setter.Value>
</Setter>
Run Code Online (Sandbox Code Playgroud)

请注意,您还可以TargetType在样式上指定属性,这样就不必为所有属性引用添加前缀WrapPanel:

<Style TargetType="WrapPanel">
Run Code Online (Sandbox Code Playgroud)


Jef*_*Son 5

您必须设置WrapPanel的Background属性!否则,WPF子系统不会将其识别为SolidColorBrush(也可能是另一个刷子).

<WrapPanel Background="White">
...
</WrapPanel>
Run Code Online (Sandbox Code Playgroud)

足够了.