简体   繁体   English

Winforms ComboBox SelectedItem更改不会影响BindingSource

[英]Winforms ComboBox SelectedItem changing does not affect the BindingSource

I am making C# / WinForms application. 我正在制作C#/ WinForms应用程序。 The problem I couldn't solve (yet) is that when I change the SelectedItem of ComboBox programatically, it is changed until the ComboBox loses the focus, after that it "reminds" its value before assigning the SelectedItem. 我无法解决的问题是,当我以编程方式更改ComboBox的SelectedItem时,它会被更改,直到ComboBox失去焦点,之后它会在分配SelectedItem之前“提醒”其值。 I think that it takes the old value from binding source. 我认为它需要来自绑定源的旧值。 When choosing an item using UI the underlying bound object is updated normally but it doesn't so when I'm assigning new value to SelectedItem programatically. 当使用UI选择项目时,底层绑定对象会正常更新,但是当我以编程方式将新值分配给SelectedItem时,情况并非如此。

Just for additional info: I am trying to implement "undo", which means I am saving every change somewhere and when Edit>>>Undo I'm reversing all these changes done by user. 只是为了获得更多信息:我正在尝试实现“撤销”,这意味着我将保存每个更改,并且当编辑>>>撤消我正在撤消用户完成的所有这些更改。 Interesting thing is that the other controls (TextBoxes, NumericUpDowns) work fine. 有趣的是,其他控件(TextBoxes,NumericUpDowns)工作正常。

Here are the Details: 以下是详细信息:

I have a ComboBox which I bind to the ComboItem object like this: 我有一个ComboBox,它绑定到ComboItem对象,如下所示:

ComboBox comboBox = new ComboBox();
List<ComboItem> items = new List<ComboItem>();
ComboList comboList = Configuration.ComboList.LoadComboList();

Combo myCombo = comboList.GetCombo(control.MemberName);
if (myCombo != null)
{
    items.Add(new ComboItem(0, "", 0.0, 0.0));
    for (int index = 0; index < myCombo.ComboItems.Count; index++)
    {
        items.Add(myCombo.ComboItems[index]);
    }
}

where Combo and ComboList are custom classes for loading the data from configuration file. 其中Combo和ComboList是用于从配置文件加载数据的自定义类。 Then I set the Display and Value members and also DataSource as well: 然后我也设置了Display和Value成员以及DataSource:

comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Key";
comboBox.DataSource = items;

"Text" and "Key" are members of ComboItem class: “Text”和“Key”是ComboItem类的成员:

public class ComboItem
{
    public int Key { get; set; }
    public string Text { get; set; }
    public double Coef1 { get; set; }
    public double Coef2 { get; set; } 

    public void CopyValues() {...}
    public override bool Equals() {...}
}

Now the problem: when executing undo I check everything needed to have all the cast operations safe and clear and trying to "undo" with this code: 现在的问题是:当执行撤消时,我检查所有需要的所有操作安全和清除并尝试使用此代码“撤消”:

Logger.Info(controls[0], op, "ExecuteUndo");
((ComboBox)controls[0]).Focus();
((ComboBox)controls[0]).SelectedItem = (ComboItem)op.GetOldValue();
Logger.Info(controls[0], "AFTER CHANGE");

Logger is just logging. 记录器只是记录。 op object is taken from undo sequence and it gives appropriate value using "GetOldValue()". op对象取自undo序列,并使用“GetOldValue()”给出适当的值。 That code actually IS AFFECTING the UI, but until the control loses its focus. 该代码实际上是影响UI,但直到控件失去焦点。 It happens on next undo which should affect other control and thus let this combobox to lose the focus. 它会在下一次撤消时发生,这会影响其他控制,从而让这个组合框失去焦点。

I am sure that this happens on comboBox_LostFocus event because first thing I do on this event is Logging and it already shows me the value that SHOULDN'T BE. 我确信这发生在comboBox_LostFocus事件上,因为我在这个事件上做的第一件事是Logging,它已经向我显示了应该不应该的值。

I think the problem you are seeing is that the ComboBox is displaying one value, but hasn't written the value to the binding source yet (which doesn't happen until you lose focus). 我认为你看到的问题是ComboBox正在显示一个值,但尚未将值写入绑定源(直到你失去焦点才会发生)。

You can try doing something like this to write the data when an item is selected (assuming there is just the one databinding associated with the ComboBox ): 你可以尝试做这样的事情来在选择项目时写入数据(假设只有一个与ComboBox关联的数据绑定):

private void comboBox_SelectedIndexChanged(object sender, EventArgs e) {
  comboBox.DataBindings[0].WriteValue();
}

And just to make sure, you either subscribe to this event from the designer, or wire it up manually: 只是为了确保,您可以从设计人员订阅此事件,也可以手动连接:

public Form1() {
  InitializeComponent();
  comboBox.SelectedIndexChanged += comboBox_SelectedIndexChanged;
}

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

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