2017-10-03 4 views
0

私は延長しましたDataGridView。私は別のアセンブリへの参照を追加することなく、ツールボックスに新しいコントロールを追加したい(右クリックオプション "アイテムの選択"を使用して)。コントロールはフォームの同じプロジェクトにあり、それらを分けたくありません。どうすればこれを達成できますか?コントロールが同じプロジェクトにあるときにツールボックスに拡張コントロールを追加する方法はありますか?

ありがとうございました。

編集:これはユーザーコントロールではない場合、ユーザーコントロールに関する質問と重複する可能性がありませんでした。

編集2:コード自体(それは仕掛品だそれは終わっていないのです。):

class BindedDataGrid<T> : DataGridView 
{ 
    public BindedDataGrid() 
    { 
     InitializeComponent(); 

     IEnumerable<PropertyInfo> properties = typeof(T).GetProperties().Where(p => Attribute.IsDefined(p, typeof(BindingValueAttribute))); 

     foreach (PropertyInfo property in properties) 
     { 
      var column = new DataGridViewColumn() 
      { 
       HeaderText = ((property.GetCustomAttributes(true)[0]) as BindingValueAttribute).Value 
      }; 

      Columns.Add(column); 
     } 
    } 
} 
+0

hmm。それはプロジェクトのコンポーネントの1つとして定義されているはずです。 ProjectName_Componentsタブに表示されます。あなたの拡張機能はUserControlから継承されていますか? –

+0

@o_O - この質問はユーザーコントロールではなく、拡張コントロールに関するものです。 – Sipo

+0

私の悪い、[これは助けるかもしれない](https://stackoverflow.com/questions/1116311/how-to-put-an-extended-winforms-control-on-toolbox) –

答えて

0
public class BindedDataGrid : DataGridView 
    { 
     public BindedDataGrid() 
     { 
     } 

     public BindedDataGrid(Type bindType) 
     { 
      IEnumerable<PropertyInfo> properties = bindType.GetProperties().Where(p => Attribute.IsDefined(p, typeof(BindingValueAttribute))); 

      foreach (PropertyInfo property in properties) 
      { 
       var prop = property.GetCustomAttributes(true)[0]; 
       if (prop is BindingValueAttribute) 
       { 
        var column = new DataGridViewColumn() 
        { 
         HeaderText = property.Name 
        }; 
        column.CellTemplate = new DataGridViewTextBoxCell(); 
        Columns.Add(column); 
       } 
      } 
     } 
    } 

    public class BindingValueAttribute : Attribute 
    { 
     public string Value { get; set; } 
    } 

    public class BindOne 
    { 
     [BindingValueAttribute()] 
     public string Name { get; set; } 

     [BindingValueAttribute()] 
     public int Age { get; set; } 
    } 
  1. すべての最初のBindedDataGridなしジェネリック
  2. が非を作成しないことを確認パラメータ化されたコンストラクタ

    private void InitializeComponent() 
    { 
        this.bindedDataGrid1 = new PlayingWithThread.BindedDataGrid(typeof(BindOne)); 
        ((System.ComponentModel.ISupportInitialize)(this.bindedDataGrid1)).BeginInit(); 
    
関連する問題