Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databinding a Custom Control

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);
    }
}
like image 470
ejmack Avatar asked Mar 03 '09 16:03

ejmack


People also ask

What is an example of simple data binding control?

The ability of a control to bind to a single data element, such as a value in a column in a dataset table. Simple data binding is the type of binding typical for controls such as a TextBox control or Label control, which are controls that typically only display a single value.

What is data bind control?

Data binding is the process that couples two data sources together and synchronizes them. With data binding, a change to an element in a data set automatically updates in the bound data set.

What is .NET databinding?

Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.

What is data binding explain with example?

The term data binding is also used in cases where an outer representation of data in an element changes, and the underlying data is automatically updated to reflect this change. As an example, a change in a TextBox element could modify the underlying data value.


1 Answers

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
like image 114
ejmack Avatar answered Oct 13 '22 03:10

ejmack