views:

563

answers:

1

I have a custom component where I have implemented INotifyPropertyChanged and IBindableComponent.

However, when I try to databind a property, the designer adds this line:

this.component11.TestString =
global::WindowsFormsApplication2.Properties.Settings.Default.Setting;

instead of creating a binding as it does with a TextBox:

this.textBox2.DataBindings.Add(new System.Windows.Forms.Binding(
   "Text",
   global::WindowsFormsApplication2.Properties.Settings.Default,
   "Setting2",
   true,
   System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

I would have thought the designer would simply look to see if IBindableComponent is implemented and if it is, generate the binding coding instead of the assignment code.

Any ideas why this works with a textbox and not my custom component?

Thanks in advance, Clint

Here is my custom component:

public partial class Component1 : Component, INotifyPropertyChanged, IBindableComponent
    {
        public Component1()
        {
            InitializeComponent();
        }

        public Component1(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        private string teststring;
        [Bindable(true)]
        public string TestString
        {
            get
            {
                return teststring;
            }
            set
            {
                if (teststring != value)
                {
                    teststring = value;
                    FirePropertyChanged("TestString");
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        void FirePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region IBindableComponent Members

        private BindingContext bindingContext = null;

        public BindingContext BindingContext
        {
            get
            {
                if (null == bindingContext)
                {
                    bindingContext = new BindingContext();
                }

                return bindingContext;
            }
            set { bindingContext = value; }
        }

        private ControlBindingsCollection databindings;

        public ControlBindingsCollection DataBindings
        {
            get
            {
                if (null == databindings)
                {
                    databindings = new ControlBindingsCollection(this);
                }
                return databindings;
            }
            set { databindings = value; }
        }

        #endregion
    }

print("code sample");
+1  A: 

Try [ DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden ), EditorBrowsable( EditorBrowsableState.Advanced ), Browsable( false ) ] public BindingContext BindingContext { ... }

[ ParenthesizePropertyName( true ), RefreshProperties( RefreshProperties.All ), DesignerSerializationVisibility( DesignerSerializationVisibility.Content ), Category( "Data" ) ] public ControlBindingsCollection DataBindings { ... }

jyoung
This didn't seem to work, the designer still generates the code:this.component11.TestString = global::TestApplicationSettings.Properties.Settings.Default.TestSetting;Instead of the correct binding code. Any other ideas to look at?Thanks for answering.
Clint
Wait, that does work. I just needed to recompile my component, delete and add it again after making your suggested changes. Thanks very much!
Clint