好吧,我正在尝试使用ColorAnimation更改DataTemplate中StackPanel的背景颜色:
<DataTemplate DataType="{x:Type logic:Sensor}">
<StackPanel Name="SensorPanel" MouseDown="SensorPanel_MouseDown">
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Status}" Value="0">
<!--<Setter TargetName="SensorPanel" Property="Background" Value="LawnGreen" />-->
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="SensorPanel"
Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
To="LawnGreen" Duration="0:0:0.25" AutoReverse="True" RepeatBehavior="4">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
没有编译时错误。但是当我运行它时会抛出InvalidOperationException:“'Background'属性不指向路径中的DependencyObject'(0)。(1)'。”
什么? :d
答案 0 :(得分:17)
您的代码对我来说非常合适。我只做了一些小修改。
<DataTemplate DataType="{x:Type Model:Sensor}">
<StackPanel Name="SensorPanel" Background="LightBlue" Width="100" Margin="5">
<TextBlock Text="{Binding Name}"/>
<ToggleButton Margin="2" IsChecked="{Binding IsChecked}" Content="Set status=0" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Status}" Value="0">
<!--<Setter TargetName="SensorPanel" Property="Background" Value="LawnGreen" />-->
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="SensorPanel"
Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
To="LawnGreen" Duration="0:0:0.25" AutoReverse="True" RepeatBehavior="4">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<ListBox ItemsSource="{Binding Sensors}" />
答案 1 :(得分:11)
出于文档目的:
使用(Panel.Background).(SolidColorBrush.Color)
有点困难。真正的问题是ColorAnimation
仅适用于Color
属性,而不适用于Brush
。对我来说这就是诀窍:
定义面板画笔......
<StackPanel Name="SensorPanel" MouseDown="SensorPanel_MouseDown">
<StackPanel.Background>
<SolidColorBrush Color="White" x:Name="PanelColor"/>
</StackPanel.Background>
</StackPanel>
...然后为Color
的{{1}}属性设置动画:
SolidColorBrush