2008-09-16 28 views
1

我有一个自定义组件,我已实施了 INotifyPropertyChangedIBindableComponent将应用程序设置数据绑定到自定义组件

然而,当我尝试进行数据绑定的属性,设计师会将此 行:

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

回答

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 { 
    ... 
} 
+0

这似乎并没有工作,设计者还生成的代码: this.component11.TestString =全球: :TestApplicationSettings.Properties.Settings.Default.TestSetting; 而不是正确的绑定代码。还有其他想法吗? 感谢您的回答。 – Clint 2008-10-07 21:09:24