2014-04-07 52 views
1

當我檢查所有三個複選框並取消選中最後一個時,第一個和最後一個都未選中。取消選中一個CheckBox取消選中三個?

其他這種神祕的行爲發生在玩弄複選框時。

已經挖了幾個小時後,在這裏的情況。

我有這樣的形式:

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));   
    }  
} 

數據的我的複選框綁定在設計時定義如下:

數據綁定 | 已檢查 | 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; } 
    } 
} 

而且在先進的數據綁定設置,更新設置爲發生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; 
} 

兩個測試通過!

我有點迷失在這裏。任何可能的問題的想法?

編輯

我已經CheckedChanged事件手工處理的複選框中this answer規定。

現在,有時候,當我檢查所有三個複選框並在之後取消選中它們時,我的按鈕仍保持啓用狀態,但它不應該保持啓用狀態。

以下是我如何手動處理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(); 
} 
+0

我認爲這是贏的形式? – Sayse

+0

是的,如標籤。 –

+3

我有一個[類似的問題](http://stackoverflow.com/questions/16980942/update-databinding-on-lost-focus-winforms)與winforms在哪裏可以克服這一點,我不得不明確重置所需的框。 Wpf沒有這個問題 – Sayse

回答

相關問題