简体   繁体   中英

c# visual studio custom control binding issue

I would like to create a simple custom control (actually a type of button). I have created the control but the step I am missing is how to add the binding. The control code I have looks like this:

public partial class PopUpButton : UserControl
{

    public PopUpButton()
    {
        InitializeComponent();
        _checked = false;
        DrawButton();
    }
    public delegate void ChangedEventHandler(object sender, EventArgs e);
    public event ChangedEventHandler OnValueChanged;
    private bool _checked;
    private String _text;

    private void UserControl1_Resize(object sender, EventArgs e)
    {
        DrawButton();
    }

    [Bindable(true)]
    public bool Checked
    {
        get
        {
            return _checked;
        }
        set
        {
            _checked = value;
            DrawButton();
            if (OnValueChanged != null)
            {
                OnValueChanged.Invoke(this, new EventArgs());
            }
        }
    }

    [Bindable(true)]
    public String DisplayText
    {
        get
        {
            return _text;
        }
        set
        {
            _text = value;
            DrawButton();
        }
    }

    private void DrawButton()
    {
        // do some stuff
    }

    private void PopUpButton_Click(object sender, EventArgs e)
    {
        _checked = !_checked;
        DrawButton();
        if (OnValueChanged != null)
        {
            OnValueChanged.Invoke(this, new EventArgs());
        }
     }
}

The call to bind to the control looks like this:

        regControl1.DataBindings.Clear();
        regControl1.DataBindings.Add("Checked", CustomButton1, "Checked");   

I know that I need to define a data source and member but cannot see how to implement this. When the above binding is called then regControl1 updates with the value of "Checked" however the function "OnValueChanged" is always null so the binding has failed, thus when "Checked" changes "regControl1" is not updated.

Ideas anyone?

Finally got something working. This solution handles a click both on the body and on the label and re-sizes the component during design. I was almost there but hope this helps:

public partial class PopUpButton : UserControl, INotifyPropertyChanged
{

    public PopUpButton()
    {
        InitializeComponent();
        _checked = false;
        DrawButton();
    }
    private bool _checked;
    private String _text;

    public event PropertyChangedEventHandler PropertyChanged;
    public event EventHandler ButtonClick;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    protected void Button_Click(object sender, EventArgs e)
    {
        _checked = !_checked;
        DrawButton();
        NotifyPropertyChanged("Checked");
        if (this.ButtonClick != null)
            this.ButtonClick(this, e);
    }

    private void UserControl1_Resize(object sender, EventArgs e)
    {
        DrawButton();
    }

    [Bindable(true)]
    public bool Checked
    {
        get
        {
            return _checked;
        }
        set
        {
            _checked = value;
            DrawButton();
            NotifyPropertyChanged("Checked");
        }
    }

    [Bindable(true)]
    public String DisplayText
    {
        get
        {
            return _text;
        }
        set
        {
            _text = value;
            DrawButton();
        }
    }

    private void HandleClick( )
    {
        _checked = !_checked;
        DrawButton();
        NotifyPropertyChanged("Checked");
    }

    private void DrawButton()
    {
        //do some drawing stuff here
    }

    private void PopUpButton_Resize(object sender, EventArgs e)
    {
        DrawButton();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM