我正在尝试绑定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工作正常,为什么不动画?
答案 0 :(得分:4)
问题不在于你如何绑定而是绑定。如果你想进入内部异常,你会看到这个错误:
无法冻结此Storyboard时间轴树以供跨线程使用。
这基本上意味着因为Storyboards
使用线程,而他们使用的所有内容都需要freezable而Bindings
不需要。 Storyboard
无法使用动画期间可能会更改的动态值。
您可以尝试将SolidColorBrush
替换为Color
,如下所示,但您也可能需要对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>