CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2002
    Posts
    788

    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??
    
       
            }
    
    }

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Create a bindable property

    Does it produce errors ¿
    What happens when you run this code ¿

  3. #3
    Join Date
    Jul 2002
    Posts
    788

    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.

  4. #4
    Join Date
    Jul 2002
    Posts
    788

    Re: Create a bindable property

    Any help!???

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

  5. #5
    Join Date
    Dec 2011
    Posts
    1

    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
  •  





Click Here to Expand Forum to Full Width

Featured