文本框/复选框的数据绑定值不正确,直到文本框/复选框被验证 [英] Databound value of textbox/checkbox is incorrect until textbox/checkbox is validated

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

问题描述

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



所以例如,我有code> bindingSource.DataSource = myDog ,其中一个复选框绑定到属性 IsSleeping ,以便当用户检查该框时, IsSleeping 变为true,当用户取消选中该框时, IsSleeping 变为false。



这工作正常。问题是,在复选框被验证之前,$ code> IsSleeping 的值不会更新,只有当焦点从复选框移动到其他位置时,才会出现 。因此,如果我想要在框被取消选中时发生某些事情:

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

myDog.IsSleeping 将仍然是真的,直到复选框的验证稍后提出。因此,当穷人 myNaughtyKitty (谁正在听 DogPropertyChanged 事件)来吃在 myDog 的食物之中 myDog 正在睡觉, myDog 真的只是醒来!



更糟糕的是, MyAnimalEntities.SaveChanges()还没有看到更改为 myDog ,所以$ code> IsSleeping 的值从不保存到数据库。移动 .SaveChanges()调用 IsSleepingCheckbox_Validated 不能解决这个问题,因为如果复选框被切换,表单被关闭,无需从复选框移除 复选框从不被验证,因此其状态永远不会保存!



我想象这是数据绑定和复选框/文本框的一个相当普遍的问题,实际上我已经在线上找到了大量的帖子,但没有人似乎有一个解决方案。 有没有人能找到解决方法?

解决方案

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



要处理此事,您可以处理 BindingSource.ListChanged 事件和移动您的 SaveChanges 代码。

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

HTH


I have a winforms checkbox that is bound to a property of an Entity Framework entity.

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.

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 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!

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?

解决方案

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.

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

HTH

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆