[英]Error message “Cannot animate '(0).(1)' on an immutable object instance” for Button with RelayCommand
I Use a extended button managed with two properties: IsLocked
and IsRequired
. 我使用由两个属性管理的扩展按钮:
IsLocked
和IsRequired
。 IsLocked
deactivates the button by using a RelayCommand
: IsLocked
使用停用按钮RelayCommand
:
public class RelayCommand : ICommand
{
private Action _execute;
private Func<bool> _canExecute;
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public RelayCommand(Action execute, Func<bool> canExecute = null)
{
this._execute = execute;
this._canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return this._canExecute == null || this._canExecute();
}
public void Execute(object parameter)
{
this._execute();
}
}
IsRequired
changes the background color of the button with an animation. IsRequired
通过动画更改按钮的背景色。
Both properties are dependent and defined like: 这两个属性都是依赖的,并且定义如下:
public Boolean IsRequired
{
get { return _isRequired; }
private set
{
if (_isRequired == value)
return;
_isRequired = value;
if (_isRequired)
this.IsLocked = false;
NotifyPropertyChanged();
}
}
public Boolean IsLocked
{
get { return _isLocked; }
private set
{
if (_isLocked == value)
return;
_isLocked = value;
if (_isLocked)
this.IsRequired = false;
NotifyPropertyChanged();
}
}
I control the button via properties in a viewmodel and a binding on the properties of the button in the associated view: 我通过视图模型中的属性以及在关联视图中按钮属性的绑定来控制按钮:
IsLocked="{Binding IsLocked}"
IsRequired="{Binding IsRequired}"
I have the error message "Cannot animate '(0).(1)' on an immutable object instance" when I set IsRequired
to True in my viewmodel. 当我在视图模型中将
IsRequired
设置为True时,出现错误消息“无法在不可变对象实例上为'(0)。(1)设置动画” 。 Normally, IsLocked
is false (via Setter of the property) but I see that the property IsEnabled
is still to False. 通常,
IsLocked
为false(通过属性的Setter),但是我看到属性IsEnabled
仍为False。 So, I have tried to change IsEnabled
in a PropertyChangedCallback
linked to the dependency property of my extended button. 因此,我试图在链接到扩展按钮的依赖属性的
PropertyChangedCallback
更改IsEnabled
。 But it's not possible, it's frozen. 但这是不可能的,它已经冻结了。
That works if I don't use the CanExecute
method of the RelayCommand
anymore and if I bind the property IsEnabled
of my extended button directly to the property IsLocked
. 这工作,如果我不使用
CanExecute
的方法RelayCommand
了,如果我绑定属性IsEnabled
直接我的扩展按钮的财产IsLocked
。
Any way to continue to use the relay command ? 有什么办法可以继续使用relay命令吗?
EDIT: This is the style of my extended button 编辑:这是我的扩展按钮的样式
<Button x:Class="Client.UserControls.ExtendedButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Client.UserControls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Button.Style>
<Style TargetType="{x:Type local:ExtendedButton}" BasedOn="{StaticResource {x:Type Button}}">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsEnabled" Value="True"/>
<Condition Property="IsRequired" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.EnterActions>
<BeginStoryboard>
<Storyboard BeginTime="00:00:00"
RepeatBehavior="Forever"
Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)">
<ColorAnimation To="Orange" Duration="0:0:1" AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</MultiTrigger.EnterActions>
</MultiTrigger>
</Style.Triggers>
</Style>
</Button.Style>
The style of the standard button is defined in another project named Theme with: 标准按钮的样式是在另一个名为Theme的项目中定义的,该项目具有:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Theme">
<Style TargetType="{x:Type Button}">
<Style.Resources>
<ResourceDictionary Source="ColorConstants.xaml"/>
</Style.Resources>
<Setter Property="Background" Value="{StaticResource DefaultBackgroundSolidColor}"/>
<Setter Property="BorderBrush" Value="White"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Height" Value="70"/>
<Setter Property="Margin" Value="1"/>
<Setter Property="Padding" Value="5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="2"
Margin="{TemplateBinding Margin}">
<TextBlock Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="Center"
Margin="{TemplateBinding Padding}"
VerticalAlignment="Center">
<ContentPresenter/>
</TextBlock>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Background" Value="{StaticResource IsEnabledBackgroundSolidColor}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This is probably because you did not set a value for the initial background brush in the style with the Storyboard : 这可能是因为您没有使用Storyboard样式设置初始背景笔刷的值:
<Style TargetType="{x:Type local:ExtendedButton}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="{StaticResource DefaultBackgroundSolidColor}"/>
<Style.Triggers>
<MultiTrigger>
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.