简体   繁体   English

有关数据绑定和修改UI控件的WinForms设计问题

[英]WinForms design question about data binding and modifying UI controls

I have a WinForms app that contains a combobox (comboBoxMain) and several other controls. 我有一个WinForms应用程序,其中包含一个组合框(comboBoxMain)和其他几个控件。 comboBoxMain is bound to an array of 5 objects (instances of a data class that contain several properties) and is used to select which of the 5 objects will have their properties displayed in the other UI controls. comboBoxMain绑定到5个对象的数组(包含多个属性的数据类的实例),并用于选择5个对象中的哪个将在其他UI控件中显示其属性。 Once the user has selected the desired values in all of the controls on my form, I have a button that will copy those values to each of the other 4 instances of the data class. 一旦用户在表单上的所有控件中选择了所需的值,便有了一个按钮,它将这些值复制到数据类的其他4个实例中的每个实例上。 A few of the controls on my form can impact the visibility, enabled/disabled state or selected value of other controls. 表单上的一些控件会影响其他控件的可见性,启用/禁用状态或选定的值。 For example, if a user were to select a value of "Disabled" from one of the comboboxes (let's call it comboBoxStatus), that would result in three other controls being disabled, a checkbox being checked and the title of a groupbox being changed. 例如,如果用户要从其中一个组合框中选择一个值“ Disabled”(将其称为comboBoxStatus),则将导致其他三个控件被禁用,一个复选框被选中以及一个组框的标题被更改。 The problem I'm having with this functionality can be described by the following scenario: 以下情况可以描述我使用此功能遇到的问题:

  1. The user selects a value of "Disabled" in comboBoxStatus (which then triggers code in its SelectionChangeCommitted() event handler that modifies the other controls as I described above). 用户在comboBoxStatus中选择一个值“ Disabled”(然后在其SelectionChangeCommitted()事件处理程序中触发代码,该代码将如上所述修改其他控件)。

  2. Next, the user clicks the button to copy the currently selected values in all of the controls to the other 4 data objects. 接下来,用户单击按钮以将所有控件中的当前选定值复制到其他4个数据对象。

  3. Finally, the user selects one of the other 4 data objects from comboBoxMain. 最后,用户从comboBoxMain中选择其他4个数据对象之一。

When this happens, I have a SelectionChangeCommitted() event handler for comboBoxMain that fires and looks at the SelectedValue in comboBoxStatus. 发生这种情况时,我为comboBoxMain有一个SelectionChangeCommitted()事件处理程序,该事件处理程序将触发并查看comboBoxStatus中的SelectedValue。 Unfortunately, it sees the default value for the backing property instead of a value of "Disabled" which was copied to the property earlier. 不幸的是,它看到后备属性的默认值,而不是先前复制到该属性的“ Disabled”值。 I have code at the end of comboBoxMain_SelectionChangeCommitted() which is supposed to check the values of those controls that could impact other things in the UI and make sure that everything gets updated appropriately. 我在comboBoxMain_SelectionChangeCommitted()末尾有代码,该代码应检查可能影响UI中其他内容的那些控件的值,并确保所有内容均得到适当更新。 This obviously doesn't happen though, since it's not seeing the correct value. 不过,这显然不会发生,因为它没有看到正确的值。 If I move that code out of the event handler and manually call it via a button press after the UI has bound the data for the currently selected data object, then it works. 如果我将代码移出事件处理程序,并在UI绑定了当前选定数据对象的数据后通过按钮手动调用它,那么它将起作用。 I can only guess that this happens because the bindings aren't updated until after comboBoxMain_SelectionChangeCommitted() has exited? 我只能猜测发生这种情况,因为直到comboBoxMain_SelectionChangeCommitted()退出后,绑定才会更新?

I hope that all makes sense. 我希望一切都有意义。 I've been playing with this for hours and have yet to completely understand what's happening or what the best solution to this problem is. 我已经玩了几个小时,但还没有完全了解正在发生的事情或解决此问题的最佳解决方案。 If you've got any thoughts, I'd appreciate hearing them. 如果您有任何想法,不胜感激。 Thanks for reading! 谢谢阅读!

通过将SelectionChangeCommitted事件处理程序中的代码移动到SelectedIndexChanged事件处理程序中,我能够解决此问题。

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

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