簡體   English   中英

C#Visual Studio自定義控件綁定問題

[英]c# visual studio custom control binding issue

我想創建一個簡單的自定義控件(實際上是一種按鈕)。 我已經創建了控件,但是缺少的步驟是如何添加綁定。 我擁有的控制代碼如下所示:

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

綁定到控件的調用如下所示:

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

我知道我需要定義一個數據源和成員,但是看不到如何實現它。 當調用上述綁定時,regControl1將使用“ Checked”的值進行更新,但是函數“ OnValueChanged”始終為null,因此綁定失敗,因此,當“ Checked”更改時,“ regControl1”不會更新。

有任何想法嗎?

終於有了工作。 該解決方案可以處理主體和標簽上的咔嗒聲,並在設計期間調整組件的大小。 我快到了,但希望對您有所幫助:

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