简体   繁体   English

聚焦时更改绑定对象不会更新TextBox

[英]Changing bound object doesn't update TextBox when focused

If I change the databound object of a currently focused TextBox , the TextBox does not display the new value. 如果我更改当前聚焦的TextBox的数据绑定对象,则TextBox不会显示新值。

Given a simple Form with a button, label, and a text box use the code below. 给定一个带有按钮,标签和文本框的简单表单,请使用以下代码。 If the user changes the text box value and tabs out of it, the text does not get reset to match the newly bound value (20 in this example). 如果用户更改了文本框的值并在其中跳出标签,则文本不会重置为匹配新绑定的值(在此示例中为20)。 However if I trigger the update via the button click event, the textbox updates fine. 但是,如果我通过按钮单击事件触发更新,则文本框会正常更新。

How can I get the textbox value to display the newly bound value (20) when the property changed event fires here? 当属性更改事件在此处触发时,如何获取文本框值以显示新绑定的值(20)?

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace BindingsUpdate
{
    public partial class Form1 : Form
    {
        private MyData _data;
        private System.Windows.Forms.BindingSource myDataBindingSource;   
        public Form1()
        {
            InitializeComponent();
            this.components = new System.ComponentModel.Container();
            this.myDataBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.myDataBindingSource.DataSource = typeof(BindingsUpdate.MyData);
            this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myDataBindingSource, "Value", true));
            this.label1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myDataBindingSource, "Value", true));

            _data = new MyData () { Value = 10.0};

            this.myDataBindingSource.DataSource = _data;

            _data.PropertyChanged += Data_PropertyChanged;
        }

        private void Data_PropertyChanged (object sender, PropertyChangedEventArgs e)
        {
            RefreshData ();
        }

        private void RefreshData ()
        {
            _data.PropertyChanged -= Data_PropertyChanged;
            _data = new MyData () {Value = 20.0};
            this.myDataBindingSource.DataSource = _data;

            //these don't seem to do anything..
            this.myDataBindingSource.ResetBindings(false);
            this.myDataBindingSource.ResetCurrentItem();

            _data.PropertyChanged += Data_PropertyChanged;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RefreshData ();
        }
    }

    public class MyData : INotifyPropertyChanged
    {
        private double _value;

        public double Value
        {
            get { return _value; }
            set
            {
                if (value.Equals(_value)) return;
                _value = value;
                OnPropertyChanged("Value");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Your DataBindings are stale since you replaced the existing DataSource. 由于您替换了现有的DataSource,因此DataBindings已过时。

Try clearing and adding them back in: 尝试清除并重新添加它们:

this.textBox1.DataBindings.Clear();
this.textBox1.DataBindings.Add(new Binding("Text", this.myDataBindingSource, "Value", true));

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

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