简体   繁体   English

在验证文本框/复选框之前,文本框/复选框的数据绑定值不正确

[英]Databound value of textbox/checkbox is incorrect until textbox/checkbox is validated

I have a winforms checkbox that is bound to a property of an Entity Framework entity. 我有一个winforms复选框,绑定到Entity Framework实体的属性。

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的checkboxSource.DataSource bindingSource.DataSource = myDog ,因此当用户选中该框时, IsSleeping变为true,当用户取消选中该框时, IsSleeping变为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. 问题是,价值IsSleeping不更新,直到该复选框被验证,仅当焦点从该复选框移开到别的东西出现。 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. myDog.IsSleeping仍然是真的,直到稍后提出复选框的Validated 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! 因此,当可怜的myNaughtyKitty (正在听DogPropertyChanged事件)来吃myDog的食物菜时,想到myDog正在睡觉, myDog真的只是醒来! 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. 更糟糕的是MyAnimalEntities.SaveChanges()还没有看到对myDog的更改,因此IsSleeping的值永远不会保存到数据库中。 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! .SaveChanges()调用移动到IsSleepingCheckbox_Validated并不能解决这个问题,因为如果复选框被切换但是表单关闭而没有将焦点从复选框移开则复选框永远不会被验证 ,因此它的状态永远不会被保存!

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. 您可以更改Binding.DataSourceUpdateMode属性OnPropertyChanged (默认为OnValidation ),这将导致数据源进行更新,当用户点击该复选框。 Unfortunately, the CheckedChanged event still fires before the data source is updated. 不幸的是,在更新数据源之前, CheckedChanged事件仍然会触发。

To deal with this, you can handle the BindingSource.ListChanged event and move your SaveChanges code there. 要处理这个问题,您可以处理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 HTH

This is the appropriate way to make this work. 这是使这项工作的适当方式。 Handle the CheckBox Validated Event 处理CheckBox验证事件

    /// <summary>
    /// Handles Check Box State if Changed
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void checkBoxBidSummaryItem_Validated(object sender, EventArgs e)
    {
        // Code to execute...
        _MyEntity.Save(_businessObject.SelectedBid);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM