2008-09-16 59 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