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

WinUI3中Checkbox无法绑定到类字段的问题求助

问题修复方案

1. 解决栈溢出核心问题

你的isBowling属性没有私有字段,get/set直接操作自身导致无限递归,这是栈溢出的根本原因。修改Bowler类:

public class Bowler : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    
    // 新增私有字段存储属性值
    private bool _isBowling;
    
    public string PlayerID;
    public string Name { get; set; }
    public int Stamina { get; set; }
    public double Overs { get; set; }
    public int Maiden { get; set; }
    public int Runs { get; set; }
    public int Wickets { get; set; }
    public int NoBall { get; set; }
    public int Wides { get; set; }
    public string LastBall { get; set; }

    public bool isBowling
    {
        get { return _isBowling; } // 返回私有字段
        set
        {
            // 仅当值变化时触发通知,避免无效更新
            if (_isBowling != value)
            {
                _isBowling = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(isBowling)));
            }
        }
    }

    public override string ToString()
    {
        return JsonConvert.SerializeObject(this);
    }
}

2. 修复DataGrid列绑定问题

第一列(DataTemplate+Checkbox)

补充UpdateSourceTrigger=PropertyChanged确保UI修改实时同步到数据源,同时新增IsEnabled绑定用于控制禁用状态:

<controls:DataGridTemplateColumn Header="Select Two">
    <controls:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding isBowling, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      IsEnabled="{Binding IsCheckBoxEnabled}"
                      Checked="CheckBox_Checked"
                      Unchecked="CheckBox_Unchecked"/>
        </DataTemplate>
    </controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>

第二列(DataGridCheckBoxColumn)

确保IsReadOnly设为False,并完善绑定参数:

<controls:DataGridCheckBoxColumn IsReadOnly="False"
                                 Header="Select 2"
                                 Binding="{Binding isBowling, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                 IsEnabledBinding="{Binding IsCheckBoxEnabled}"/>

3. 实现选中数量限制逻辑

第一步:给Bowler类添加禁用状态属性

private bool _isCheckBoxEnabled = true;
public bool IsCheckBoxEnabled
{
    get { return _isCheckBoxEnabled; }
    set
    {
        if (_isCheckBoxEnabled != value)
        {
            _isCheckBoxEnabled = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsCheckBoxEnabled)));
        }
    }
}

第二步:在页面代码中处理选中/取消事件

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    UpdateCheckBoxStates();
}

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    UpdateCheckBoxStates();
}

private void UpdateCheckBoxStates()
{
    int selectedCount = bowlingTeam.Count(b => b.isBowling);
    foreach (var bowler in bowlingTeam)
    {
        // 已选中的保持启用,未选中的在选中数量≥2时禁用
        bowler.IsCheckBoxEnabled = bowler.isBowling || selectedCount < 2;
    }
}

4. 初始化集合的异常修复

现在isBowling属性已修复私有字段,初始化时设置bowler.isBowling = false不会再触发栈溢出,原代码可正常使用。

关键注意事项

  • 所有绑定属性必须搭配私有字段,避免递归调用;PropertyChanged事件仅在值变化时触发,减少无效更新。
  • 双向绑定必须显式设置Mode=TwoWay,复选框建议添加UpdateSourceTrigger=PropertyChanged确保实时同步。
  • 控制复选框禁用状态需通过新增属性实现,不能直接在UI中硬编码逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 06:12:05