2011-01-04 12 views
0

私はVBのコントロールの配列の古いユーザーです。それは今行っているので、私は交換を探しています。C#/ Silverlightでユーザーコントロールの配列を作成する/ユーザーコントロールのインデックスを作成する方法

Silverlight用にC#でユーザーコントロールを作成しました。このコントロールは、boolプロパティIsCheckedを公開します。子ウィンドウで

<ImageButton Name"imgbutton1" IsChecked="True"/> 
<ImageButton Name"imgbutton2" IsChecked="False"/> 
<ImageButton Name"imgbutton3" IsChecked="False"/> 

私は1つ、にisCheckedは=のために、falseにtrueに設定されたこと、他をクリックしたときに、私は、すなわち、これらの一連の制御を移入し、ラジオボタンの同等としてそれらを使用します

foreach (ImageButton imgbutton in imgButtons) imgbutton.IsChecked = false; 
imgbuttons[index].IsChecked = true; 
:、私のような何かをするだろう彼らにisCheckedが、(いくつかの「インデックス」変数が指し示す)私は必要がある1つのすべてをクリアするために、次に

List<ImageButton> imgButtons= new List<ImageButton>(); 
imgButtons.Add(imgbutton1); 
imgButtons.Add(imgbutton2); 
imgButtons.Add(imgbutton3); 

:私のような何かを考えています

私が持っている問題は、List<ImageButton>がコンパイルされていないということです。私はコレクションで非常に薄れており、私が紛失しているものを理解することはできません。私は標準的なコントロールのために行うことができますが、私はそこにユーザーコントロールを取得することはできません。

ありがとう!

PS:私はRadioButtonコントロールをカスタマイズしたいと思っていましたが、Blendを持っていないので、この画像コントロールを他のタイプのコントロールに使用しています。しかし、これを実装するより良い方法があると思うなら、私に教えてください。

EDIT:コンパイラは、型または名前空間名 『のImageButton』が見つかりませんでした」と言う(あなたがusingディレクティブまたはアセンブリ参照が不足している?)」あなたはコンパイルエラーが何であったかを教えていなかった

答えて

3

あなたへの参照を持っていることを確認してくださいコード内の "ImageButton"コントロール( "使用する"部分)。あなたは、なぜそれを行うには

自動的にあなたは自分のコード内であなたの「のImageButton」で右クリックし、「解決」をクリックすることができ、それが自動的にちょうど何か他のものを考えリファレンス

+0

ありがとう、これは私が行方不明だったものです。同じ名前空間内にすべて存在するので、私が作成したコントロールへの参照を追加しなければならないかどうかはわかりませんでした。私はそれを子ウィンドウに追加してから、コードの裏にも見えると仮定していました。私はXAMLのリファレンスを見ることができます。 – PaulG

2

あなたが言ったときを「リストがコンパイルされていません」。

私の推測では、あなたのファイルにリストコレクションの名前空間を含める必要があるということです。

using System.Collections.Generic; 
+0

ありがとう、私のmsgを編集しました。 – PaulG

1

が追加されます、私はわかりません表示されているImageButtonsのリスト(その子ウィンドウ内にある)を保持します。それらがすべて同じコンテナ内にある場合は、次のようなものを使用できます。

// here "grd" is your Grid container, it could be another type of container though 
foreach (ImageButton imgBtn in grd.Children.OfType<ImageButton>()) 
       imgBtn.IsChecked = false; 
+0

私はインデックス作成のためのリストを保管しています。これらのイメージボタンはすべてRadioButtonのように動作し、選択した項目を呼び出し元に渡します。これらのボタンの1つを設定する必要がある場合は、コントロール名ではなくインデックスを呼び出します。意味がありますか? – PaulG

+0

grd.Children.IndexOf(imgBtn);を使用して、子オブジェクトの実際のインデックスを取得できます。インデックス付きの同じアイテムを取得する:grd.Children [i]。 – danbord

+0

これも面白い解決策です。私は、呼び出し元のインデックスとgrd.Children.IndexOf(imgButton)の間のマッピングを作成する必要があります。 – PaulG

-1

これは役に立ちます。

ユーザーコントロールのためのコード:

で現在
//usercontrol1.cs 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing; 

namespace WindowsFormsApplication1 
{ 
    public partial class UserControl1 : UserControl 
    { 
     Color formcolor; 
     public UserControl1() 
     { 
      InitializeComponent(); 

     } 


     public void setvals(string a1,string a2,string a3,string a4) 
     { 
      t1.Text=a1; 
      t2.Text=a2; 
      t3.Text=a3; 
      t4.Text=a4; 
     } 

     public Color formColor 
     { 
      get 
      { 
       return formcolor; 
      } 
      set 
      { 
       formcolor = value; 
       this.BackColor = formcolor; 
      } 
     } 

    } 
} 





usercontrol.designer.cs 

namespace WindowsFormsApplication1 
{ 
    partial class UserControl1 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Component Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.t1 = new System.Windows.Forms.TextBox(); 
      this.t2 = new System.Windows.Forms.TextBox(); 
      this.t3 = new System.Windows.Forms.TextBox(); 
      this.t4 = new System.Windows.Forms.TextBox(); 
      this.SuspendLayout(); 
      // 
      // t1 
      // 
      this.t1.Location = new System.Drawing.Point(20, 16); 
      this.t1.Name = "t1"; 
      this.t1.Size = new System.Drawing.Size(100, 20); 
      this.t1.TabIndex = 0; 
      // 
      // t2 
      // 
      this.t2.Location = new System.Drawing.Point(20, 42); 
      this.t2.Name = "t2"; 
      this.t2.Size = new System.Drawing.Size(100, 20); 
      this.t2.TabIndex = 1; 
      // 
      // t3 
      // 
      this.t3.Location = new System.Drawing.Point(20, 68); 
      this.t3.Name = "t3"; 
      this.t3.Size = new System.Drawing.Size(100, 20); 
      this.t3.TabIndex = 2; 
      // 
      // t4 
      // 
      this.t4.Location = new System.Drawing.Point(20, 94); 
      this.t4.Name = "t4"; 
      this.t4.Size = new System.Drawing.Size(100, 20); 
      this.t4.TabIndex = 3; 
      // 
      // UserControl1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.Controls.Add(this.t4); 
      this.Controls.Add(this.t3); 
      this.Controls.Add(this.t2); 
      this.Controls.Add(this.t1); 
      this.Name = "UserControl1"; 
      this.Size = new System.Drawing.Size(278, 133); 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private System.Windows.Forms.TextBox t1; 
     private System.Windows.Forms.TextBox t2; 
     private System.Windows.Forms.TextBox t3; 
     private System.Windows.Forms.TextBox t4; 
    } 
} 

は完全に動的この

グローバル変数

private UserControl1[] userControl11=new WindowsFormsApplication1.UserControl1[3]; 

今ようそのユーザーコントロールの配列を作成することができユーザーコントロールの配列

ボタンで書き込むことができます

this.SuspendLayout(); 
      Random r=new Random(DateTime.Now.Millisecond); 
      for (int i = 0; i < 3; i++) 
      { 

       userControl11[i] = new UserControl1(); 
       this.userControl11[i].formColor = Color.FromArgb(r.Next(255),r.Next(255),r.Next(255)); 
       this.userControl11[i].Location = new System.Drawing.Point(133 , 133*(i+1)); 
       this.userControl11[i].Name = "userControl11"; 
       this.userControl11[i].Size = new System.Drawing.Size(278, 133); 
       this.userControl11[i].TabIndex = 0; 
       this.Controls.Add(this.userControl11[i]); 
      } 
      ; 
関連する問題