1

我有一个已实现的自定义组件, INotifyPropertyChanged并且IBindableComponent.

但是,当我尝试对属性进行数据绑定时,设计器会添加以下行:

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

而不是像使用 TextBox 那样创建绑定:

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

我原以为设计者会简单地查看是否 IBindableComponent已实现,如果是,则生成绑定编码而不是分配代码。

任何想法为什么这适用于文本框而不是我的自定义组件?

这是我的自定义组件:

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");
4

1 回答 1

2

尝试:

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

[ ParenthesizePropertyName( true ),
  RefreshProperties( RefreshProperties.All ),
  DesignerSerializationVisibility( DesignerSerializationVisibility.Content ),
  Category( "Data" ) ]
public ControlBindingsCollection DataBindings {
   ...
}
于 2008-09-17T03:31:41.377 回答