Home > Software design >  How to add a property to a control's DataBindings
How to add a property to a control's DataBindings

Time:02-10

I want to write a control derived from CheckedListBoxControl (DevExpress), and I need to add a property to the (DataBindings)

These are the standard Properties shown in the PropertyGrid:

enter image description here

So I can only choose between Tag and Text.
What I want is to add a third option called gttMasterField (which will be of type int, don't know if this matters)

I have been experimenting with the documentation but with no results.
These don't seem to cover exact what I am looking for, I don't know the correct search terms to find this, which makes it difficult to google for it. It will probably be somewhere in the documentation but also there I don't know on what terms to look for.

Create a Windows Forms user control that supports simple data binding
Create a Windows Forms user control that supports lookup data binding
Create a Windows Forms user control that supports complex data binding

Here is some code with comments that will also help to explain what I am searching for

public partial class gttDXManyToManyCheckedListBox : CheckedListBoxControl
{
    private int _gttMasterField;
  
    // This I want populated by setting the binding property MasterField
    public int gttMasterField
    {
        get { return _gttMasterField; }
        set { _gttMasterField = value; }
    }
}

The project is a dotnet framework 4.7.2

CodePudding user response:

To make a custom Property appear in the PropertyGrid's (DataBindings), decorate the Property with the BindableAttribute set to true:

[Bindable(true)]
public int gttMasterField
{
    get { return _gttMasterField; }
    set { _gttMasterField = value; }
}

Optionally also decorate with the desired DesignerSerializationVisibilityAttribute attribute

[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public int gttMasterField
{
    get { return _gttMasterField; }
    set { _gttMasterField = value; }
}

The class can also specify the default bindable Property, settings a DefaultBindingPropertyAttribute:

[DefaultBindingProperty("gttMasterField")]
public partial class gttDXManyToManyCheckedListBox : CheckedListBoxControl
{
    private int _gttMasterField;

    [Bindable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public int gttMasterField
    {
        get { return _gttMasterField; }
        set { _gttMasterField = value; }
    }
}

Important note:
the class should implement INotifyPropertyChanged -> a bindable Property is supposed to raise notification events. When the binding is set in the Designer, a BindingSource is generated to mediate the binding, but it requires that the objects involved send change notifications (mostrly to determine when the Property value is updated, usually as DataSourceUpdateMode.OnPropertyChanged).

For example:

using System.Runtime.CompilerServices;

[DefaultBindingProperty("gttMasterField")]
public partial class gttDXManyToManyCheckedListBox : CheckedListBoxControl, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private int _gttMasterField;

    [Bindable(true)]
    public int gttMasterField
    {
        get { return _gttMasterField; }
        set { _gttMasterField = value; NotifyPropertyChanged(); }
    }

    private void NotifyPropertyChanged([CallerMemberName] string PropertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
    }
}

Setting DesignerSerializationVisibility.Content implies initialization. If a BindingSource is used, this object supports initialization on itself, the Attribute is not strictly required.
It could be set to DesignerSerializationVisibility.Hidden, though, depending on the use case.

  • Related