0

と の 2 つのプロパティで管理される拡張ボタンを使用しIsLockedますIsRequiredIsLockedを使用してボタンを無効にします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(プロパティのセッター経由で) false ですが、プロパティIsEnabledがまだ False になっていることがわかります。そのため、拡張ボタンの依存関係プロパティへのリンクを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