You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何根据_Set1与_Set2系列属性设置ButtonDisabled的状态?

实现方案

核心逻辑很明确:
ButtonDisabled 的值等于 !(至少一个_Set1为true 且 至少一个_Set2为true),也就是:

ButtonDisabled = !( (CheckBox1_Set1 || CheckBox2_Set1 || CheckBox3_Set1) && (CheckBox1_Set2 || CheckBox2_Set2 || CheckBox3_Set2) );

接下来分两种常见场景实现:

1. 普通类场景(无属性变更通知)

如果这些属性在普通类中,每次修改 _Set1 或 _Set2 系列属性后,手动调用方法更新 ButtonDisabled 即可:

public bool CheckBox1_Set1 { get; set; }
public bool CheckBox2_Set1 { get; set; }
public bool CheckBox3_Set1 { get; set; }
public bool CheckBox1_Set2 { get; set; }
public bool CheckBox2_Set2 { get; set; }
public bool CheckBox3_Set2 { get; set; }
public bool ButtonDisabled { get; set; }

// 调用此方法更新按钮状态
public void UpdateButtonDisabledState()
{
    bool hasSet1Checked = CheckBox1_Set1 || CheckBox2_Set1 || CheckBox3_Set1;
    bool hasSet2Checked = CheckBox1_Set2 || CheckBox2_Set2 || CheckBox3_Set2;
    ButtonDisabled = !(hasSet1Checked && hasSet2Checked);
}

示例调用:

// 修改复选框属性后更新按钮状态
CheckBox1_Set1 = true;
UpdateButtonDisabledState();

2. MVVM场景(带属性变更通知)

如果是WPF/MAUI等MVVM场景,通常会实现 INotifyPropertyChanged 接口,这里提供两种实现方式:

方式一:在属性setter中直接触发更新

public class ViewModel : INotifyPropertyChanged
{
    private bool _checkBox1_Set1;
    public bool CheckBox1_Set1
    {
        get => _checkBox1_Set1;
        set
        {
            _checkBox1_Set1 = value;
            OnPropertyChanged(nameof(CheckBox1_Set1));
            UpdateButtonDisabledState();
        }
    }

    private bool _checkBox2_Set1;
    public bool CheckBox2_Set1
    {
        get => _checkBox2_Set1;
        set
        {
            _checkBox2_Set1 = value;
            OnPropertyChanged(nameof(CheckBox2_Set1));
            UpdateButtonDisabledState();
        }
    }

    private bool _checkBox3_Set1;
    public bool CheckBox3_Set1
    {
        get => _checkBox3_Set1;
        set
        {
            _checkBox3_Set1 = value;
            OnPropertyChanged(nameof(CheckBox3_Set1));
            UpdateButtonDisabledState();
        }
    }

    private bool _checkBox1_Set2;
    public bool CheckBox1_Set2
    {
        get => _checkBox1_Set2;
        set
        {
            _checkBox1_Set2 = value;
            OnPropertyChanged(nameof(CheckBox1_Set2));
            UpdateButtonDisabledState();
        }
    }

    private bool _checkBox2_Set2;
    public bool CheckBox2_Set2
    {
        get => _checkBox2_Set2;
        set
        {
            _checkBox2_Set2 = value;
            OnPropertyChanged(nameof(CheckBox2_Set2));
            UpdateButtonDisabledState();
        }
    }

    private bool _checkBox3_Set2;
    public bool CheckBox3_Set2
    {
        get => _checkBox3_Set2;
        set
        {
            _checkBox3_Set2 = value;
            OnPropertyChanged(nameof(CheckBox3_Set2));
            UpdateButtonDisabledState();
        }
    }

    private bool _buttonDisabled;
    public bool ButtonDisabled
    {
        get => _buttonDisabled;
        set
        {
            _buttonDisabled = value;
            OnPropertyChanged(nameof(ButtonDisabled));
        }
    }

    private void UpdateButtonDisabledState()
    {
        bool hasSet1Checked = CheckBox1_Set1 || CheckBox2_Set1 || CheckBox3_Set1;
        bool hasSet2Checked = CheckBox1_Set2 || CheckBox2_Set2 || CheckBox3_Set2;
        ButtonDisabled = !(hasSet1Checked && hasSet2Checked);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

方式二:用MVVM工具包简化为计算属性

如果使用CommunityToolkit.Mvvm工具包,可以通过注解自动处理属性变更通知:

using CommunityToolkit.Mvvm.ComponentModel;

public partial class ViewModel : ObservableObject
{
    [ObservableProperty]
    private bool checkBox1_Set1;

    [ObservableProperty]
    private bool checkBox2_Set1;

    [ObservableProperty]
    private bool checkBox3_Set1;

    [ObservableProperty]
    private bool checkBox1_Set2;

    [ObservableProperty]
    private bool checkBox2_Set2;

    [ObservableProperty]
    private bool checkBox3_Set2;

    // 自动依赖所有_Set1/_Set2属性,变化时自动通知UI更新
    public bool ButtonDisabled => !( (CheckBox1_Set1 || CheckBox2_Set1 || CheckBox3_Set1) && (CheckBox1_Set2 || CheckBox2_Set2 || CheckBox3_Set2) );
}

内容的提问来源于stack exchange,提问作者TheBoubou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 16:20:55