views:

53

answers:

2

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?

A: 

remove the if.

if (value != this.m_Pets)
sadboy
could you explain this a little? I am having trouble understanding why or how that would be the problem.
Refracted Paladin
This condition looks perfectly fine to me. The point of it obviously is to not raise the `PropertyChanged` event if the new value of the property does not differ from its previous value.
stakx
+1  A: 
tbxListPets.DataBindings.Add("Text",this, Pets);
//                                        ^^^^

shouldn't it rather be:

tbxListPets.DataBindings.Add("Text", this, "Pets");
//                                         ^^^^^^

Note the missing double-quotation marks in your original code.

You need to specify the name of the property (which is to be bound to the UI control) as a string. WinForms will then be able to retrieve that property's value either through reflection, or through the TypeDescriptor mechanism.

If you omit the quotation marks, then an attempt will be made to bind the UI control to a property whose name equals the current value of Pets (which is almost definitely not what you intended).

See also: ControlBindingsCollection.Add reference page on MSDN

stakx
Boy I feel dumb. Thank you for taking time to teach me to "Tie my shoes..."
Refracted Paladin
You're welcome. No need to feel dumb, mate. That's the kind of error that I'd hardly notice if I'd written the code myself, while others would see it fairly quickly.
stakx