wpf 背景属性未指向路径“(0).(1)”中的依赖项对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17399210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:06:53  来源:igfitidea点击:

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

c#wpfanimationtriggersstyles

提问by Maya

I wrote this code and got an exception:

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

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

背景属性未指向路径“(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?

有什么帮助吗?

回答by dlev

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 TargetTypeproperty on your style, so that you don't have to prefix all property reference with WrapPanel:

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

<Style TargetType="WrapPanel">

回答by JeffRSon

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 属性!否则 WPF 子系统不会将其识别为 SolidColorBrush(也可能是另一个画笔)。

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

is sufficient.

足够了。