【问题标题】:WPF Color animation binding TO to SolidColorBrush.ColorWPF 颜色动画绑定到 SolidColorBrush.Color
【发布时间】:2013-06-09 14:26:57
【问题描述】:

我正在尝试绑定 ColorAnimation TO={Binding Source={StaticResource Test1}, Path=Color}

我可以使用 SolidColorBrush 执行此操作,但尝试在彩色动画中使用它时出现“System.Windows.Markup.XamlParseException”异常。

这是我所拥有的:

<SolidColorBrush x:Key="Test1" Color="{Binding Source={x:Static self:MySettings.Default}, Path=HighlightColor}"/>
<SolidColorBrush x:Key="Test2" Color="{Binding Source={x:Static self:MySettings.Default}, Path=TextColor}"/>

<Style x:Key="WindowLabel" TargetType="Border" BasedOn="{StaticResource HighlightBorder}">
    <Setter Property="Background" Value="{StaticResource Test2}"/>
    <Setter Property="Height" Value="50"/>
    <Style.Triggers>
        <EventTrigger RoutedEvent="MouseEnter">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation To="{Binding Source={StaticResource Test1}, Path=Color}" 
                                    Duration="{StaticResource AnimSpeed}" 
                                    Storyboard.TargetProperty="Background.Color"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="MouseLeave">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation To="{Binding Source={StaticResource Test2}, Path=Color}" 
                                    Duration="{StaticResource AnimSpeed}" 
                                    Storyboard.TargetProperty="Background.Color"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>
</Style>

Test2 SolidColorBrush 可以正常工作,为什么动画不能?

【问题讨论】:

  • 我刚刚尝试更改 To="Red" 并且效果很好,所以它必须是绑定,但是为什么呢?

标签: wpf animation colors


【解决方案1】:

问题不在于你如何绑定,而在于你如何绑定。如果你进入内部异常,你会看到这个错误:

无法冻结此 Storyboard 时间线树以供跨线程使用。

这基本上意味着因为Storyboards 使用线程,它们以及它们使用的所有东西都需要可冻结,而Bindings 则不需要。 Storyboard 不能使用在动画过程中可能发生变化的动态值。

您可以尝试用Color 替换SolidColorBrush,如下所示,但您可能还需要用Duration 做一些事情

<Color  A="255" R="100" G="0" B="0" x:Key="Test1"/>
<Color A="255" R="0" G="100" B="0" x:Key="Test2"/>
...
<Style TargetType="Border">
  <Setter Property="Background">
      <Setter.Value>
          <SolidColorBrush Color="{StaticResource Test2}"/>
      </Setter.Value>
  </Setter>
  <Setter Property="Height" Value="50"/>
  <Style.Triggers>
      <EventTrigger RoutedEvent="MouseEnter">
          <BeginStoryboard>
              <Storyboard>
                  <ColorAnimation To="{StaticResource Test1}"
                  Duration="0:0:1" 
                  Storyboard.TargetProperty="Background.Color"/>
              </Storyboard>
          </BeginStoryboard>
      </EventTrigger>
      <EventTrigger RoutedEvent="MouseLeave">
          <BeginStoryboard>
              <Storyboard>
                  <ColorAnimation To="{StaticResource Test2}" 
                  Duration="0:0:1" 
                  Storyboard.TargetProperty="Background.Color"/>
              </Storyboard>
          </BeginStoryboard>
      </EventTrigger>
  </Style.Triggers>
</Style>

【讨论】:

  • 效果很好,但我想做的是动态更改颜色。使用您的解决方案,我仍在使用静态资源。我可以将 R、G、B 绑定到任何东西吗?
  • 从设置加载后是否更改颜色/画笔?
猜你喜欢
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-18
  • 2021-02-19
  • 2010-10-05
  • 2021-11-10
相关资源
最近更新 更多