0

我使用通过两个属性管理的扩展按钮:IsLockedIsRequired. IsLocked使用 a 停用按钮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}"

当我在视图模型中设置为 True时,出现错误消息“无法在不可变对象实例上设置动画 '(0).(1)'” 。IsRequired通常,IsLocked是假的(通过属性的设置器),但我看到该属性IsEnabled仍然是假的。所以,我试图改变IsEnabled一个PropertyChangedCallback链接到我的扩展按钮的依赖属性。但这是不可能的,它被冻结了。

如果我不再使用 的CanExecute方法,RelayCommand并且如果我将IsEnabled扩展按钮的属性直接绑定到属性,那将有效IsLocked

有什么办法可以继续使用中继命令?

编辑:这是我的扩展按钮的样式

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

4

1 回答 1

0

这可能是因为您没有为Storyboard 样式中的初始背景画笔设置值:

<Style TargetType="{x:Type local:ExtendedButton}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="{StaticResource DefaultBackgroundSolidColor}"/>
    <Style.Triggers>
        <MultiTrigger>
             ...
于 2019-03-18T08:09:04.203 回答