组合框的WinForms变化的SelectedItem不影响的BindingSource [英] Winforms ComboBox SelectedItem changing does not affect the BindingSource

查看:144
本文介绍了组合框的WinForms变化的SelectedItem不影响的BindingSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做C#/ WinForms应用程序。我没能解决(但)问题是,当我改变ComboBox的编程的SelectedItem,这是改变,直到组合框失去焦点,之后分配的SelectedItem在这之前提醒它的价值。我认为,它从绑定源旧值。当选择使用UI项目底层绑定的对象正常更新,但它不会,所以当我指定新的值以编程的SelectedItem。

I am making C# / WinForms application. 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. 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.

只是为了更多的信息:我想实现撤消,这意味着我的地方保存每一个变化,当编辑>>>撤消我扭转这些变化由用户来完成。有趣的是,其他控件(文本框,NumericUpDowns)做工精细。

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.

下面是详细内容:

我有,我绑定到的ComboItem对象这样的组合框:

I have a ComboBox which I bind to the ComboItem object like this:

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

在这里组合和ComboList是从配置文件中加载数据的自定义类。然后,我设置显示和Value成员和数据源也还有:

where Combo and ComboList are custom classes for loading the data from configuration file. Then I set the Display and Value members and also DataSource as well:

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

文本和密钥是的ComboItem类的成员:

"Text" and "Key" are members of ComboItem class:

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() {...}
}

现在的问题:当执行撤消我检查有所有的转换操作安全,清晰,并试图撤销这一code所需的一切:

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

日志只是记录。操作对象是从撤销序列拍摄并使用GetOldValue()给出了适当的值。这code实际上是影响了用户界面,但直到控制失去了焦点。它发生在接下来的撤消这应该不会影响其他的控制,从而让这个组合框失去焦点。

Logger is just logging. op object is taken from undo sequence and it gives appropriate value using "GetOldValue()". That code actually IS AFFECTING the UI, but until the control loses its focus. It happens on next undo which should affect other control and thus let this combobox to lose the focus.

我相信这种情况在comboBox_LostFocus事件,因为我对本次活动做的第一件事就是登录,它已经具备了我不应该的价值。

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.

推荐答案

我认为你所看到的问题是,组合框显示的是一个数值,但不值写入到绑定源,但(这不会发生,直到你失去了焦点)。

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

当一个项目被选中,您可以尝试做这样的东西可写数据(假设只是关联的数据绑定的组合框

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

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

这篇关于组合框的WinForms变化的SelectedItem不影响的BindingSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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