views:

57

answers:

2

I am trying to make a (very) simple Data Binding test, but it doesnt work as I expected... Say I have the following classes:

// this class represents some kind of data producer
public class DataSourceClass
    {
        public string Data { get; set; }

        public DataSourceClass()
        { }
    }


//this form holds the TextBox control as the Data consumer
public partial class DatabindingTestForm : Form
    {
        public DataSourceClass ds { get; set; }
        public DatabindingTestForm()
        {
            InitializeComponent();
            ds = new DataSourceClass();
            textBox.DataBindings.Add("Text", ds, "Data");
        }

        private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox.Checked)
                ds.Data = "CHECKED";
            else
                ds.Data = "NOT CHECKED";
        }
    }

I didnt add the designer code, but its there, and the form holds a TextBox object and a CheckBox object. As you can understand, I am trying to make the Textbox Text property change as the user checks \ unchecks the CheckBox. But this code doesnt update the TextBox Text property. can someone please explain me what am I missing?

+4  A: 

You need some way to notify WinForms when the value of the Data property changes. The most straightforward way is to either:

  • Add an event to DataSourceClass: public event EventHandler DataChanged;
  • Make DataSourceClass implement INotifyPropertyChanged. This gives you a PropertyChanged event.

Either way you'll have a new event that you need to raise. You'll need to convert your Data property from an auto property to one with a private field, a get method and a set method. Once you have an explicit getter and setter for the Data property, you'll be able to raise your event from inside the setter.

Tim Robinson
I got a filling that I am missing the main idea... why do I need to use data binding if I need to add an event anyway? I could have just use the event (no ControlBindingsCollection necessary...)
ET
For an example this simple, you could just have an event that updates the text box directly. However, in a real application, you want to keep the data model (your `DataSourceClass`) separate from the user interface (the text box). The purpose of data binding is to sit in between the model and the UI, passing data between them without the need to make them aware of each other.
Tim Robinson
+2  A: 

You can use the INotifyPropertyChanged interface. I didn't run this through the IDE/compiler so there could be a syntax error.

public class DataSourceClass : INotifyPropertyChanged
{ 
    private string _data;

    public string Data
    {
        get
        {
            return _data;
        }  
        set
        {
            if( _data != value )
            {
                _data = value;
                OnPropertyChanged( "data" );
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged( string propertyName )
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if( handler != null )
        {
            handler( new PropertyChangedEventArgs( propertyName ) );
        }
    }
} 
Jerod Houghtelling