取消选中一个CheckBox取消选中三个?

时间:2014-04-07 16:25:37

标签: c# winforms data-binding checkbox inotifypropertychanged

当我选中所有三个复选框并取消选中最后一个复选框时,第一个和最后一个复选框都取消选中。

使用复选框时会出现其他类似的神秘行为。

在挖了几个小时之后,就是这里的情景。

我有这样的表格:

enter image description here

复选框是使用BindingSource绑定的数据。 BindingSource绑定到我的域对象。

询问

public class Inquiry {
    public virtual IList<Report> Reports{ get; protected set; }
    public bool DomaineMedicament { 
        get { hasReport(ReporType.DomaineMedicament); }
        set { 
            if (value) addReport(ReportType.DomaineMedicament);
            else removeReport(ReportType.DomaineMedicament);
        }
    }
    // Same code for the other bound boolean values...          

   private void addReport(ReportType report) {
       // Can only contain one for each type at any time
       if (hasReport(report)) return; 

       Report adding = new Report() { Type = report, Inquiry=this; };
       Reports.Add(adding);
   } 

   private bool hasReport(ReportType report) {
       return 0 < Reports.Count(r => r != null && r.Type == report);
   }

   private void removeReport(ReportType report) {
       if (hasReport(report)) 
           Reports.Remove(Rapports.Single(r => r.Type == report));           
   }       
}

我的复选框的数据绑定在设计时定义如下:

DataBindings | 已检查 | inquiryBindingSource - DomaineMedicament

表格

public partial class NouvelleDemandeForm : Form {
    public NouvelleDemandeForm() { 
        InitializeComponent();
        inquiredAtDateTimePicker.MaxDate = DateTime.Today.AddDays(-1);
        inquiryBindingSource.CurrentItemChanged += (sender, e) => {
            createNewInquiryButton.Enabled = 
                DataContext.AllRequiredInformationIsProvided;
        };
    }

    public DemandeAccesInformation DataContext { 
        get { return (DemandeAccesInformation)inquiryBindingSource.DataSource; }
        set { inquiryBindingSource.DataSource = value; } 
    }
}

在高级DataBindings设置中,更新设置为OnPropertyChanged

我调查过使用断点,还写了单元测试,我无法找出发生了什么。

相关单元测试

[TestClass]
public class InquiryTests{
    [TestMethod]
    public void IShouldCountThreeWhenAddingThreeReports() {
        givenIHaveANewInquiry();
        whenIAddTheThreeReports();
        thenIShouldCount(3);
    }

    [TestMethod]
    public void IShouldCountTwoWhenAddingThreeReportsAndRemovingOne() {
        givenIHaveANewInquiry();
        whenIAddTheThreeReports();
        whenIRemove(ReportType.RegistreDesRefus);
        thenIShouldCount(2);
    }

    private void givenIHaveANewInquiry() { inquiry = new Inquiry(); }

    private void whenIAddTheThreeReports() {
        inquiry.DomaineMedicament = true;
        inquiry.OrdonnanceElectronique = true;
        inquiry.RegistreDesRefus = true;
    }

    private void whenIRemove(ReportType report) {
        inquiry.Reports.Remove(inquiry.Reports.Single(r => r.Type == report));
    }

    private void thenIShouldCount(int expectedReportCount) {
        Assert.AreEqual(expectedReportCount, inquiry.Reports.Count);
    }

    private Inquiry inquiry;
}

两个测试都通过了!

我有点迷失在这里。有关任何可能问题的想法吗?

  

修改

我已按照this answer中的说明手动处理了CheckedChanged事件复选框。

现在,有时候,当我检查所有三个复选框并在之后取消选中它们时,我的按钮仍保持启用状态,而不是。

以下是我手动处理CheckedChanged事件的方法。

electronicPrescriptionCheckBox.CheckedChanged += (sender, e) => {
    addRemoveReport(electronicPrescriptionCheckBox.Checked
        , RapportAccesInformation.TypeRapport.OrdonnanceElectronique);
};

private void addRemoveReport(bool addRemove
    , ReportType report) {
    switch (report) {
        case ReportType.DomaineMedicament:  
            DataContext.DomaineMedicament = addRemove; 
            break;
        case ReportType.OrdonnanceElectronique:  
            DataContext.OrdonnanceElectronique = addRemove; 
            break;
        case ReportType.RegistreDesRefus:   
            DataContext.RegistreDesRefus = addRemove; 
            break;
    }

    inquiryBindingSource.ResetCurrentItem();
}

1 个答案:

答案 0 :(得分:0)

根据@Sayse链接的这个问题,这似乎是一个WinForms DataBinding漏洞。

Update Databinding on lost focus (Winforms)

我最终对CheckBox.CheckedChanged事件进行了手动处理,并确保每次CheckedChanged发生时,对象实际上都反映了复选框的状态。

相关问题