Click to See Complete Forum and Search --> : Create a bindable property


mce
February 5th, 2009, 10:56 PM
I want to create a bindable property for a button class that is derived from System.Windows.Forms.Button.



class MyButton : System.Windows.Forms.Button
{

public MyButton()
{

}
int _bExternalOn = 0;


[System.ComponentModel.Bindable(true)]
public int ExternalCmd
{
set {
_bExternalOn = value;
}
get
{
return _bExternalOn;
}
}

}

//in form, button1 is an instance of MyButton

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.DataBindings.Add(new Binding("ExternalCmd", testSrc, "SRC_VALUE"));

}

Src testSrc = new Src();
public class Src
{
int nVal = 0;
public int SRC_VALUE
{
get { return nVal; }
set { nVal = value; }
}
}

private void button2_Click(object sender, EventArgs e)
{
button1.ExternalCmd = 66;//testSrc.SRC_VALUE should show 66 now as this is bind to button1.ExternalCmd , but it doesn;'t. why??


}

}

HanneSThEGreaT
February 6th, 2009, 12:14 AM
Does it produce errors ¿
What happens when you run this code ¿

mce
February 6th, 2009, 12:49 AM
It doesn't have any error, the result is the same as if i didn't do the binding
as in

button1.DataBindings.Add(new Binding("ExternalCmd", testSrc, "SRC_VALUE"));



What i wanted is that changes in ExternalCmd be reflected in testSrc as this is supposed be bind with ExternalCmd., and changes in testSrc need to be reflected in ExternalCmd as well. Currently non of this is happening.

mce
February 6th, 2009, 01:20 AM
Any help!???

I have looked at my codes for half a day to no avail!

ohoseini
December 26th, 2011, 07:51 AM
Hi,

I know it is very late, but I answer your question :D (maybe it is useful for someone else)

Your "MyButton" class must implement INotifyPropertyChanged to raise the property changed each time your Bindable property changed. So you must change your class like this:

class MyButton : System.Windows.Forms.Button, INotifyPropertyChanged
{
.
.
.

[System.ComponentModel.Bindable(true)]
public int ExternalCmd
{
set {
_bExternalOn = value;
}
get
{
return _bExternalOn;
RaisePropertyChanged("ExternalCmd");
}
}

public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}