简体   繁体   English

C#Visual Studio自定义控件绑定问题

[英]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. 当调用上述绑定时,regControl1将使用“ Checked”的值进行更新,但是函数“ OnValueChanged”始终为null,因此绑定失败,因此,当“ Checked”更改时,“ regControl1”不会更新。

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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