|
-
February 5th, 2009, 11:56 PM
#1
Create a bindable property
I want to create a bindable property for a button class that is derived from System.Windows.Forms.Button.
Code:
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??
}
}
-
February 6th, 2009, 01:14 AM
#2
Re: Create a bindable property
Does it produce errors ¿
What happens when you run this code ¿
-
February 6th, 2009, 01:49 AM
#3
Re: Create a bindable property
It doesn't have any error, the result is the same as if i didn't do the binding
as in
Code:
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.
-
February 6th, 2009, 02:20 AM
#4
Re: Create a bindable property
Any help!???
I have looked at my codes for half a day to no avail!
-
December 26th, 2011, 08:51 AM
#5
Re: Create a bindable property
Hi,
I know it is very late, but I answer your question (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));
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|