2008-09-16 12 views
1

INotifyPropertyChangedIBindableComponentを実装したカスタムコンポーネントがあります。カスタムコンポーネントへのデータバインディングApplicationSettings

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

私が持っているでしょう:それはテキストボックスと同じように結合を作成するのではなく、

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

:私はプロパティをデータバインドしようとすると、

しかし

は、設計者は、この 行を追加しますデザイナーは単に 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

+0

待って、それは動作します。私はちょうどあなたの提案された変更を加えた後、私のコンポーネントを再コンパイルし、削除して、それを再び追加する必要がありました。どうもありがとう! – Clint

関連する問題