英文:
WPF Exception when try animating binding foreground
问题
我有这个控件...:
<iconPack:PackIconAlvo
Width="{StaticResource iconSize}"
Height="{StaticResource iconSize}"
HorizontalAlignment="Center"
Foreground="{Binding IsMonitoringButtonActive, Converter={StaticResource IsSelectedToColorConverter}}"
Kind="Monitoring"
Style="{StaticResource AlarmAnimation}" />
...使用了这个转换器...:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? new SolidColorBrush(Color.FromRgb(0, 255, 161)) : new SolidColorBrush(Color.FromRgb(131, 131, 131));
}
...和这个动画样式:
<Style x:Key="AlarmAnimation">
<Style.Triggers>
<DataTrigger Binding="{Binding IsAlarmForMonitoringButton}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
AutoReverse="True"
RepeatBehavior="Forever"
Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)"
From="Gray"
To="Red"
Duration="0:0:0.5" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
当我启动动画(IsAlarmForMonitoringButton == true)并且 IsMonitoringButtonActive == false 时,我遇到了错误:
无法在不可变对象实例上执行动画 'Foreground.(0)'。
我无法解决这个错误。在这个应用程序中,前景是可变的很重要,当IsAlarmForMonitoringButton==true时它也很重要。
如何处理这个问题?
英文:
I have this control…:
<iconPack:PackIconAlvo
Width="{StaticResource iconSize}"
Height="{StaticResource iconSize}"
HorizontalAlignment="Center"
Foreground="{Binding IsMonitoringButtonActive, Converter={StaticResource IsSelectedToColorConverter}}"
Kind="Monitoring"
Style="{StaticResource AlarmAnimation}" />
…with this converter…:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? new SolidColorBrush(Color.FromRgb(0, 255, 161)) : new SolidColorBrush(Color.FromRgb(131, 131, 131));
}
…and this animation style:
<Style x:Key="AlarmAnimation">
<Style.Triggers>
<DataTrigger Binding="{Binding IsAlarmForMonitoringButton}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
AutoReverse="True"
RepeatBehavior="Forever"
Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)"
From="Gray"
To="Red"
Duration="0:0:0.5" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
When I start animation (IsAlarmForMonitoringButton == true) and IsMonitoringButtonActive == false, then I have error:
> Cannot animate 'Foreground.(0)' on an immutable object instance.
I can't deal with this error. It is important in this application that the foreground is variable, it is also important that it is animated when IsAlarmForMonitoringButton==true.
How can I handle this problem?
答案1
得分: 2
使用Style
而不是使用绑定表达式和转换器来设置Foreground
属性,例如:
<iconPack:PackIconAlvo
Width="{StaticResource iconSize}"
Height="{StaticResource iconSize}"
HorizontalAlignment="Center"
Kind="Monitoring">
<iconPack:PackIconAlvo.Style>
<Style TargetType="iconPack:PackIconAlvo">
<Setter Property="Foreground" Value="Yellow" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsMonitoringButtonActive}" Value="True">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding IsAlarmForMonitoringButton}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
AutoReverse="True"
RepeatBehavior="Forever"
Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)"
From="Gray"
To="Red"
Duration="0:0:0.5" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</iconPack:PackIconAlvo.Style>
</iconPack:PackIconAlvo>
另一种选项是以编程方式应用动画。
英文:
Set the Foreground
property using a Style
instead of using a binding expression and a converter, e.g.:
<iconPack:PackIconAlvo
Width="{StaticResource iconSize}"
Height="{StaticResource iconSize}"
HorizontalAlignment="Center"
Kind="Monitoring">
<iconPack:PackIconAlvo.Style>
<Style TargetType="iconPack:PackIconAlvo">
<Setter Property="Foreground" Value="Yellow" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsMonitoringButtonActive}" Value="True">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding IsAlarmForMonitoringButton}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
AutoReverse="True"
RepeatBehavior="Forever"
Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)"
From="Gray"
To="Red"
Duration="0:0:0.5" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</iconPack:PackIconAlvo.Style>
</iconPack:PackIconAlvo>
The other option is to apply the animation programmatically.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论