I am trying to implement some very simple Data Binding in my User Control that runs in a WinForm app. The User Control is extended like so -->
public partial class ucDiagnosis : XtraUserControl, INotifyPropertyChanged
Then on that User Control I have this Property -->
private string m_Pets;
public string Pets
{
get
{
return m_Pets;
}
set
{
if (value != this.m_Pets)
{
this.m_Pets = value;
NotifyPropertyChanged("Pets");
}
}
}
Then in the Constructor I have this -->
tbxListPets.DataBindings.Add("Text",this, Pets);
Next I have this in my Events region -->
public event PropertyChangedEventHandler PropertyChanged;
Next I have this Method -->
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
Finally, in my method to Load Data into the Control I have this -->
var additionalDiagnosis = BLLMcp.GetPlanDiagnosisAddtional(planID);
Pets = additionalDiagnosis.Rows[0]["Pets"].ToString();
The program runs but the text box stays empty. No matter what I type in it, it just clears out instantly. Also, I put a breakpoint on the Setter which gets called on the initial load and has the correct data assigned to value but it does not update the text box. Furthermore, the Setter never gets called again no matter what I type in the box.
Further Attempt by me
So I seem to have gotten it working but I am perplexed as to how, if it even truly is. I simply added this line with the other two in my LoadData() method -->
tbxListPets.Text = addtionalDiagnosis.Pets;
Why would I have to set the TextBox explicitly after setting the Property? Isn't that what Data Binding does for me or have I somehow managed to screw up and only implement one-way binding?