简体   繁体   English

如何将 go 返回到之前在 combobox 中选择的条目?

[英]How to go back to previously selected entry in a combobox?

In my application there is a requirement like this:在我的应用程序中有这样的要求:

In a form there is a combo box and 4 textboxes.在一个表单中有一个组合框和 4 个文本框。 The combo box always contain range of values 1...10.组合框始终包含值范围 1...10。 Based on the value selected in the combo box, I need to read 4 data items corresponding to the selected value from a database and display these values in the 4 textboxes.根据在组合框中选择的值,我需要从数据库中读取与所选值对应的 4 个数据项,并将这些值显示在 4 个文本框中。 Thus for each value in the combo box, there are 4 values in the database.因此对于组合框中的每个值,数据库中有 4 个值。

Also, user can change these values by editing the values in 4 text boxes.此外,用户可以通过编辑 4 个文本框中的值来更改这些值。

As an example assume that currently selected value in the combo box is 1. So the data corresponding to record 1 is read from the database and shown in the 4 text boxes.例如,假设组合框中当前选择的值为 1。因此从数据库中读取对应于记录 1 的数据并显示在 4 个文本框中。 If the user don't edit any value in the textbox and selects entry 2 (or any other value than 1) then the data corresponding to the newly selected value (ie 2) is fetched from the database and displayed in the 4 textboxes.如果用户不编辑文本框中的任何值并选择条目 2(或除 1 以外的任何其他值),则从数据库中获取与新选择的值(即 2)对应的数据并显示在 4 个文本框中。 But if the user edited any value by typing into any of the four available textboxes then those new values corresponding to record 1 should be saved to the database before switching to the newly selected entry, ie 2.但是,如果用户通过在四个可用文本框中的任何一个中键入来编辑任何值,那么在切换到新选择的条目(即 2)之前,应将与记录 1 对应的那些新值保存到数据库中。

To do this I wrote some code in the "SelectedIndexChanged event handler" of the combo box.为此,我在组合框的“SelectedIndexChanged 事件处理程序”中编写了一些代码。 There I check whether the user has changed any value.我在那里检查用户是否更改了任何值。 If yes, I will prompt the user to save the data.如果是,我会提示用户保存数据。 After saving the data, the combo box will show the newly selected value by user ie 2 (and the 4 text boxes will show data corresponding to record 2).保存数据后,组合框会显示用户新选择的值即2(4个文本框会显示记录2对应的数据)。 All the above mentioned functionalities work fine.所有上述功能都可以正常工作。 But there is an additional requirement "If the save operation to the database fails, then the initial value in the combo box should be shown along with the user edited data (in the textboxes) which means if the database write fails I have to show the currently selected value 1 without switching to the newly selected value 2".但是还有一个额外的要求“如果对数据库的保存操作失败,那么组合框中的初始值应该与用户编辑的数据(在文本框中)一起显示,这意味着如果数据库写入失败我必须显示当前选择的值 1,而不切换到新选择的值 2"。

In the SelectedIndexChanged handler I tried writing the below code.在 SelectedIndexChanged 处理程序中,我尝试编写以下代码。

If (DatabaseWriteFails)
{
    ComboBox->SelectedIndex = previous_value; (previous_value is 1 in our example)
}

But the above code did not work (I think since we are trying to change the selected value from SelectedIndexChanged handler itself).但是上面的代码不起作用(我想因为我们试图从 SelectedIndexChanged 处理程序本身更改选定的值)。 When we enter the SelectedIndexChanged handler I can see that ComboBox->SelectedValue is already changed to the newly selected value ie 2.当我们进入 SelectedIndexChanged 处理程序时,我可以看到 ComboBox->SelectedValue 已经更改为新选择的值,即 2。

The question is "how we can go back to the previous entry once we enter the SelectedIndexChanged handler"?问题是“一旦我们进入 SelectedIndexChanged 处理程序,我们如何才能 go 回到上一个条目”? Any help is appreciated.任何帮助表示赞赏。 Thank you.谢谢你。

First of all, if you change the ComboBox value from the event, it will trigger that event again, so instead of using SelectedIndexChanged use SelectionChangeCommitted which occurs only when the user changes the selected item from UI, not programmatically.首先,如果您更改事件中的 ComboBox 值,它将再次触发该事件,因此不要使用 SelectedIndexChanged,而是使用 SelectionChangeCommitted,它仅在用户从 UI 更改所选项目时发生,而不是以编程方式发生。

Second, you should change the dropdown by using either SelectedItem or SelectedIndex, from my experience SelectedValue binding doesn't update the ComboBox.其次,您应该使用 SelectedItem 或 SelectedIndex 更改下拉列表,根据我的经验,SelectedValue 绑定不会更新 ComboBox。

My advice for you as mentioned in the comments is to use bindings or even ReactiveUI .如评论中所述,我对您的建议是使用绑定甚至ReactiveUI

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

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