views:

992

answers:

3

I have a Custom Control (Windows Form) that is a lookup text box. A property on the Control is Current Selection which is a Custom Object containing "Identifier", "Code" and "Description". This property is Databound using a BindingSource.

Displaying the information works great. On the other hand regardless of whether I set the Update to OnValidate or OnValueChange it never updates the BindingSource. Is there something I'm missing to get this to auto update?

private System.Windows.Forms.BindingSource buildPlanComponentDataBindingSource;

    public void LoadBuildPlan(string itemNumber)
    {
        var buildPlanComponents = BuildPlan.LoadBuildPlanComponents(itemNumber, AutomaticPrice);
        buildPlanComponentDataBindingSource.DataSource = buildPlanComponents;
        AssemblyNumber = itemNumber;
    }




[Bindable(true)]
[DefaultValue(null)]
public ILookupSelection CurrentSelection
{
    get
    {
        if (currentSelection == null)
            currentSelection = new LookupSelection {Code = txtLookup.Text};

        return currentSelection;
    }

    set
    {
        if (value == null) return;

        currentSelection = value;

        SetText(currentSelection, DisplayText);
        SetDescription(currentSelection, DisplayDescription);
    }
}
A: 

Displaying the information works great. On the other hand regardless of whether I set the Update to OnValidate or OnValueChange it never updates the BindingSource.

Looking at your code I'm actually not sure of this. In your set, you test for null and abandon; if the data actually contains null (which is what you're describing) your control will be out of synch. I wonder if perhaps that check is masking the underlying problem.

overslacked
The only time that is really ever NULL is when the control is initially created. Otherwise this will only be set when a Selection is made which contains a value.
ejmack
If that's the case then I would recommend you use a different flag to test, other than value == null. Databinding to properties is a back and forth process ... at any rate, I would clean this bit up, but I'm also not positive it'll help your problem in the end.
overslacked
+2  A: 

Implementing INotifyPropertyChanged seems to be the solution!

    #region IPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (null != PropertyChanged)
        {
            PropertyChanged(this, e);
        }
    }

    #endregion
ejmack
IPropertyChanged should read INotifyPropertyChanged. I copied the code and got fooled by this detail when defining the inheritance. Otherwise this is great, +1
Marcel
A: 

Maybe you need to cause the DataBinding to write its value for each control whose value you are setting this way?

Assuming one data binding for a textbox named txtMySetValue:

txtMySetValue.DataBindings[0].WriteValue();

Michael