2017-01-03 5 views
0

ユーザーコントロールのプロパティ、は、私が持っている子ユーザ制御では、私は、私は以下のような子ユーザーコントロールを追加してい、別のユーザーコントロールを追加していているユーザーコントロールを持っている

ucSubMenu menu = new ucSubMenu(this); 
    pnBox.Controls.Add(menu); 

にアクセスできません。親ユーザーコントロールを初期化するプロパティープロシージャー。だから私は以下のように親ユーザーコントロールオブジェクトを取っていた子ユーザーコントロールのコンストラクタ、

private UserControl parentUserControl; 

    public UserControl ParentUserControl 
    { 
     get { return parentUserControl; } 
     set { parentUserControl = value; } 
    } 
public ucSubMenu(UserControl uc) 
    { 
     InitializeComponent(); 
     switch (Sys.ToString(uc.GetType())) 
     { 
      case "ucReport1": 
       ParentUserControl = uc as ucReport1; 

       MessageBox.Show(Sys.ToString(parentUserControl.GetType())); 


       ReportClass rc = parentUserControl.reportBindingSource.Current as ReportClass; 
       //menuBindingSource.DataSource = rc.ItemList; 
       break; 
     } 
    } 

に私は、親ユーザーは、コンストラクタのようにパブリックプロパティを制御する方法 ReportClass rc = parentUserControl.reportBindingSource.Current as ReportClass; //menuBindingSource.DataSource = rc.ItemList;

にアクセスすることはできません子ユーザコントロールから親ユーザコントロールのプロパティにアクセスできますか?

答えて

0

UserControlというオブジェクトでオブジェクトが宣言されているため、コンパイル時にそのクラスのプロパティのみがわかります。実行時に発生する「実際の」コントロールのプロパティを使用するには、このように、型キャストを実行する必要があります。

ucReport1 reportCtl = (ucReport1)parentUserControl; 

はその後 reportCtl.reportBindingSourceがそうでなければ、私は上記キャスト右のタイプを使用することを想定し(コンパイルを使用するかを必要です)。

+0

ucReport1 ucR = ucとしてucReport1; 'ParentUserControl = ucR'これは私にとってうまく動作しません。 –

関連する問題