0

The objective here to is use BindingSource + Object as a DataSource and a TextBox to display property value of the object.

The problem that i am facing, is that the TextBox is not reflecting the property values changes of the underlying object.

As the Chinese say one picture equals a thousand words, so below demo code that demonstrates the issue i am facing.

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

So when clicking the btnAssignDataSource button the textbox gets updated. I would expect that when the bound property change(btnChangePropertyValue), the change would be reflected to the textbox.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Yiorgos
  • 151
  • 1
  • 11

1 Answers1

0

have you tried this this.textBox1.DataBindings.Add("Text", dsSource, "BindingName");

sagar
  • 177
  • 3
  • 19
  • Can you try to reset the binding after you call the btnChangePropertyValue event. – sagar Apr 15 '13 at 21:34
  • Yes tried that from the beginning did not work though. The only way to get this working is to remove the binding from the textbox and add it again. The property event change is fired, so that part is working. Possibly something is related with the binding to the textbox. – Yiorgos Apr 16 '13 at 08:49
  • Adding this after the property change is working `this.textBox1.DataBindings[0].ReadValue();` But the goal is this to happen automatically, that is the purpose of binding after all. Otherwise we would not need any binding we can update the UI manually. – Yiorgos Apr 16 '13 at 08:51