本文介绍了文本框/复选框的数据绑定值不正确,直到文本框/复选框被确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到一个实体框架实体的属性的WinForms复选框。

因此​​,举例来说,我有 bindingSource.DataSource = myDog 绑定到该属性复选框 IsSleeping ,所以当用户检查中, IsSleeping 为真,当用户取消选中复选框, IsSleeping 为假。

So for example, I have bindingSource.DataSource = myDog with a checkbox bound to the property IsSleeping, so that when the user checks the box, IsSleeping becomes true, and when the user unchecks the box, IsSleeping becomes false.

这工作得很好。问题是, IsSleeping 未更新,直到复选框被验证,当焦点移动的只出现远的从复选框别的东西的价值。因此,如果我想事情发生时没有选中该复选框:

This works fine. The problem is that the value of IsSleeping is not updated until the checkbox is validated, which only occurs when focus moves away from the checkbox to something else. Thus, if I want something to happen when the box is unchecked:

private void IsSleepingCheckbox_CheckedChanged(object sender, EventArgs e)
{
    OnDogPropertyChanged(myDog);
    MyAnimalEntities.SaveChanges();
}

myDog.IsSleeping 将仍然是真实的,直到复选框的验证是后来提高。因此,当差 myNaughtyKitty (谁在听 DogPropertyChanged 事件)的说到吃出 myDog 的菜盘的思维 myDog 正在睡觉, myDog 其实只是醒来! 哦,不!

myDog.IsSleeping will still be true, until the checkbox's Validated is later raised. Thus, when poor myNaughtyKitty (who is listening to the DogPropertyChanged event) comes to eat out of myDog's food dish thinking myDog is sleeping, myDog is really just waking up! Oh no!

更糟糕的是 MyAnimalEntities.SaveChanges()不会看到更改为 myDog 然而,这样 IsSleeping 的值永远不会保存到数据库中。移动 .SaveChanges()呼叫 IsSleepingCheckbox_Validated 不解决这个问题,因为如果该复选框被切换,但随后的形式而没有移动焦点从复选框的离开的关闭,的复选框从不进行验证,因此它的状态永远不会保存!

Even worse, MyAnimalEntities.SaveChanges() does not see the changes to myDog yet, so the value of IsSleeping is never saved to the database. Moving the .SaveChanges() call to IsSleepingCheckbox_Validated does not solve this problem, because if the checkbox is toggled but then the form is closed without ever moving focus away from the checkbox, the checkbox is never validated and thus its state is never saved!

我想这一定是一个相当普遍的问题,数据绑定和复选框/文本框,而事实上我发现一吨关于这个问题的帖子在网上,但是没有人似乎有一个解决方案。 有没有人能够找到一个解决方法吗?

I imagine this must be a fairly common problem with databinding and checkboxes/textboxes, and indeed I've found a ton of posts on the subject online, but no one ever seems to have a solution. Has anyone been able to find a workaround for this?

推荐答案

您可以修改Binding.DataSourceUpdateMode属性 OnPropertyChanged (默认为 OnValidation ),这将导致对数据源进行更新时,用户点击复选框。不幸的是,的CheckedChanged 事件仍会触发前的数据源更新。

You can change the Binding.DataSourceUpdateMode property to OnPropertyChanged (the default is OnValidation), which will cause the data source to be updated when the user clicks the checkbox. Unfortunately, the CheckedChanged event still fires before the data source is updated.

要解决这个问题,你可以处理的BindingSource.ListChanged事件并移动的SaveChanges code那里。

To deal with this, you can handle the BindingSource.ListChanged event and move your SaveChanges code there.

bindingSource = new BindingSource();
bindingSource.DataSource = myDog;
checkBox1.DataBindings.Add(new Binding("Checked", bindingSource, "IsSleeping"));
checkBox1.DataBindings[0].DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;

bindingSource.ListChanged += new ListChangedEventHandler(bindingSource_ListChanged);

心连心

这篇关于文本框/复选框的数据绑定值不正确,直到文本框/复选框被确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:41