WPF尝试动画绑定前景时出现异常。

huangapple go评论70阅读模式
英文:

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…:

&lt;iconPack:PackIconAlvo
	Width=&quot;{StaticResource iconSize}&quot;
	Height=&quot;{StaticResource iconSize}&quot;
	HorizontalAlignment=&quot;Center&quot;
	Foreground=&quot;{Binding IsMonitoringButtonActive, Converter={StaticResource IsSelectedToColorConverter}}&quot;
	Kind=&quot;Monitoring&quot;
	Style=&quot;{StaticResource AlarmAnimation}&quot; /&gt;

…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:

&lt;Style x:Key=&quot;AlarmAnimation&quot;&gt;
	&lt;Style.Triggers&gt;
		&lt;DataTrigger Binding=&quot;{Binding IsAlarmForMonitoringButton}&quot; Value=&quot;True&quot;&gt;
			&lt;DataTrigger.EnterActions&gt;
				&lt;BeginStoryboard&gt;
					&lt;Storyboard&gt;
						&lt;ColorAnimation
							AutoReverse=&quot;True&quot;
							RepeatBehavior=&quot;Forever&quot;
							Storyboard.TargetProperty=&quot;Foreground.(SolidColorBrush.Color)&quot;
							From=&quot;Gray&quot;
							To=&quot;Red&quot;
							Duration=&quot;0:0:0.5&quot; /&gt;
					&lt;/Storyboard&gt;
				&lt;/BeginStoryboard&gt;
			&lt;/DataTrigger.EnterActions&gt;
		&lt;/DataTrigger&gt;
	&lt;/Style.Triggers&gt;
&lt;/Style&gt;

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.:

&lt;iconPack:PackIconAlvo
        Width=&quot;{StaticResource iconSize}&quot;
        Height=&quot;{StaticResource iconSize}&quot;
        HorizontalAlignment=&quot;Center&quot;
        Kind=&quot;Monitoring&quot;&gt;
    &lt;iconPack:PackIconAlvo.Style&gt;
        &lt;Style TargetType=&quot;iconPack:PackIconAlvo&quot;&gt;
            &lt;Setter Property=&quot;Foreground&quot; Value=&quot;Yellow&quot; /&gt;
            &lt;Style.Triggers&gt;
                &lt;DataTrigger Binding=&quot;{Binding IsMonitoringButtonActive}&quot; Value=&quot;True&quot;&gt;
                    &lt;Setter Property=&quot;Foreground&quot; Value=&quot;Red&quot; /&gt;
                &lt;/DataTrigger&gt;
                &lt;DataTrigger Binding=&quot;{Binding IsAlarmForMonitoringButton}&quot; Value=&quot;True&quot;&gt;
                    &lt;DataTrigger.EnterActions&gt;
                        &lt;BeginStoryboard&gt;
                            &lt;Storyboard&gt;
                                &lt;ColorAnimation
                                    AutoReverse=&quot;True&quot;                                
                                    RepeatBehavior=&quot;Forever&quot;
                                    Storyboard.TargetProperty=&quot;Foreground.(SolidColorBrush.Color)&quot;
                                    From=&quot;Gray&quot;
                                    To=&quot;Red&quot;
                                    Duration=&quot;0:0:0.5&quot; /&gt;
                            &lt;/Storyboard&gt;
                        &lt;/BeginStoryboard&gt;
                    &lt;/DataTrigger.EnterActions&gt;
                &lt;/DataTrigger&gt;
            &lt;/Style.Triggers&gt;
        &lt;/Style&gt;
    &lt;/iconPack:PackIconAlvo.Style&gt;
&lt;/iconPack:PackIconAlvo&gt;

The other option is to apply the animation programmatically.

huangapple
  • 本文由 发表于 2023年4月4日 16:16:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定