【问题标题】:binding textbox to object property via bindingsource not refreshing通过绑定源将文本框绑定到对象属性不刷新
【发布时间】:2013-04-15 15:45:52
【问题描述】:

这里的目的是使用 BindingSource + Object 作为 DataSource 和 TextBox 来显示对象的属性值。

我面临的问题是 TextBox 没有反映底层对象的属性值变化。

正如中国人所说的一张图片等于一千个单词,所以下面的演示代码演示了我面临的问题。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.CompilerServices;


namespace TextBoxDataSourceBindingDemo
{


    public partial class Form1 : Form
    {
        private BindingSource dsSource;
        private BindingDemoClass bDemoClass;
        public Form1()
        {
            InitializeComponent();
            bDemoClass = new BindingDemoClass();
            dsSource = new BindingSource();

            bDemoClass.BindingName = "DemoBinding";
            this.textBox1.DataBindings.Add(new Binding("Text", dsSource, "BindingName",true,DataSourceUpdateMode.OnPropertyChanged));
        }

        private void btnAssignDataSource_Click(object sender, EventArgs e)
        {
           //Setting the datasource is not enough to 
           //update the related textbox
           //refresh must be explicity called ?
           dsSource.DataSource = bDemoClass;
           dsSource.ResetBindings(true);
        }

        private void btnChangePropertyValue_Click(object sender, EventArgs e)
        {
            //Here after setting the property value
            //the textbox should be update with the new value; correct ?
            bDemoClass.BindingName = "DemoBinding2";
        }
    }
    //Demo class used as datasource  
    public class BindingDemoClass : INotifyPropertyChanged
    {
        private string _BindingName = String.Empty;

        public string BindingName
        {
            get { return _BindingName; }
            set
            {
                if (!String.Equals(_BindingName, value))
                {
                    _BindingName = value;
                    NotifyPropertyChanged();
                }
            }
        }

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}

因此,当单击 btnAssignDataSource 按钮时,文本框会更新。 我希望当绑定属性更改(btnChangePropertyValue)时,更改将反映到文本框。

【问题讨论】:

    标签: c# object binding textbox bindingsource


    【解决方案1】:

    你试过这个 this.textBox1.DataBindings.Add("Text", dsSource, "BindingName");

    【讨论】:

    • 调用 btnChangePropertyValue 事件后能否尝试重置绑定。
    • 是的,但从一开始就尝试过,但没有奏效。使其正常工作的唯一方法是从文本框中删除绑定并再次添加。触发属性事件更改,因此该部分正在工作。可能与文本框的绑定有关。
    • 在属性更改后添加这个是有效的this.textBox1.DataBindings[0].ReadValue(); 但目标是自动发生,这毕竟是绑定的目的。否则我们不需要任何绑定,我们可以手动更新 UI。
    猜你喜欢
    • 2018-11-16
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多