簡體   English   中英

錯誤消息“無法在不可變對象實例上為'(0)。(1)'設置動畫”的按鈕,帶有RelayCommand

[英]Error message “Cannot animate '(0).(1)' on an immutable object instance” for Button with RelayCommand

我使用由兩個屬性管理的擴展按鈕: IsLockedIsRequired 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通過動畫更改按鈕的背景色。

這兩個屬性都是依賴的,並且定義如下:

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();
        }
    }

我通過視圖模型中的屬性以及在關聯視圖中按鈕屬性的綁定來控制按鈕:

IsLocked="{Binding IsLocked}"
IsRequired="{Binding IsRequired}"

當我在視圖模型中將IsRequired設置為True時,出現錯誤消息“無法在不可變對象實例上為'(0)。(1)設置動畫” 通常, IsLocked為false(通過屬性的Setter),但是我看到屬性IsEnabled仍為False。 因此,我試圖在鏈接到擴展按鈕的依賴屬性的PropertyChangedCallback更改IsEnabled 但這是不可能的,它已經凍結了。

這工作,如果我不使用CanExecute的方法RelayCommand了,如果我綁定屬性IsEnabled直接我的擴展按鈕的財產IsLocked

有什么辦法可以繼續使用relay命令嗎?

編輯:這是我的擴展按鈕的樣式

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

標准按鈕的樣式是在另一個名為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>

這可能是因為您沒有使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM