【问题标题】:Changing bound object doesn't update TextBox when focused聚焦时更改绑定对象不会更新 TextBox
【发布时间】:2014-08-27 22:05:59
【问题描述】:

如果我更改当前焦点 TextBox 的数据绑定对象,则 TextBox 不会显示新值。

给定一个带有按钮、标签和文本框的简单表单,使用下面的代码。如果用户更改了文本框的值和制表符,文本不会被重置以匹配新绑定的值(本例中为 20)。但是,如果我通过按钮单击事件触发更新,则文本框更新正常。

当属性更改事件在此处触发时,如何获取文本框值以显示新绑定的值 (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));
        }
    }
}

【问题讨论】:

    标签: c# winforms data-binding


    【解决方案1】:

    您的 DataBindings 已过时,因为您替换了现有的 DataSource。

    尝试清除并重新添加它们:

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

    【讨论】:

    • 谢谢。我希望 BindingSource.ResetBindings 能为我做这件事。在我的应用程序中,我将不得不手动清除屏幕上的所有文本框,这很痛苦。
    • @kbeal2k 我认为像这样不断更改数据源并不常见。您可能需要重新考虑该策略。
    • 即使我直接更改_data.Value并且不更改数据源也会出现此问题。
    • @kbeal2k 那不应该。确保您在添加绑定时使用 DataSourceUpdateMode.OnPropertyChanged 值。
    • 我试图解决的总体问题。如果输入无效,我想做一些自定义验证并将文本框重置为以前的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多