【发布时间】:2014-05-20 00:30:51
【问题描述】:
当我选中所有三个复选框并仅取消选中最后一个复选框时,第一个和最后一个都将被取消选中。
在玩弄复选框时会发生其他此类神秘行为。
挖了几个小时后,这里是场景。
我有这个表格:
复选框是使用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; }
}
}
并且在高级 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();
}
【问题讨论】:
-
我猜这是赢单?
-
我有一个带有 winforms 的similar issue,为了克服这个问题,我必须明确重置所需的框。 wpf没有这个问题
-
@Sayse,我已经编辑了我的问题以反映这些变化。现在,当我打算通过取消选中三个复选框来删除所有报告时,看起来只有一个报告不希望被删除,也就是说,从
CheckBox的角度来看,第三个报告。我真的不明白。我整天都在研究这个错误。 =( -
您是否检查过发件人是否与您刚刚取消选中的按钮相同?如果不是,这可能是你的问题:)(假设我之前的链接有帮助!)
-
编辑到上面:您当前检查的是 checkboxes 变量而不是发件人,我的意思是您可能希望在发件人时忽略此更新,如果我没记错的话,可能为空(PS我发布的链接也花了我几天时间:p)
标签: c# winforms data-binding checkbox inotifypropertychanged