2011-01-04 4 views
0

나는 VB에서 컨트롤 배열의 옛날 사용자이다. 이제 사라 졌으므로 교체를 찾고 있습니다.C#/Silverlight에서 사용자 정의 컨트롤의 배열/인덱싱 사용자 컨트롤을 만드는 방법은 무엇입니까?

Silverlight 용 C#으로 사용자 정의 컨트롤을 만들었습니다. 이 컨트롤은 bool 속성 IsChecked를 노출합니다. 자식 창에서

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

, 나는 들어,의 IsChecked = true를 false로 설정됩니다이 그 어떤 다른 이러한 컨트롤의 시리즈를 채우고 난 하나를 클릭하면 즉 RadioButtons를 동등한,로 사용

foreach (ImageButton imgbutton in imgButtons) imgbutton.IsChecked = false; 
imgbuttons[index].IsChecked = true; 
: 내가 좋아하는 일을 할 것입니다 자신의 IsChecked하지만 (일부 '인덱스'변수가 가리키는) 내가해야 할 일을 모두 취소, 그리고

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

: 내가 좋아하는 일을 생각한다

내가 가지고있는 문제는 List<ImageButton>가 컴파일되지 않는다는 것입니다. 나는 콜렉션에서 매우 색다른 데, 내가 무엇을 놓치고 있는지 알 수 없다. 표준 컨트롤에서 할 수는 있지만 사용자 컨트롤을 얻을 수는 없습니다.

감사합니다.

추신 : 저는 RadioButton 컨트롤을 사용자 정의하는 것을 고려해 보았지만 Blend를 사용하지 않았으며 다른 유형의 컨트롤에이 이미지 컨트롤을 사용하고 있습니다. 그러나이를 구현할 수있는 더 좋은 방법이 있다고 생각되면 알려 주시기 바랍니다.

편집 : 컴파일러는 형식 또는 네임 스페이스 이름 '하여 ImageButton이'을 (를) 찾을 수 없습니다 "라는 (당신은 using 지시문 또는 어셈블리 참조가?) '당신은 컴파일러 오류가 무엇인지 말해주지 않았다

답변

3

당신이에 대한 참조가 있는지 확인하여 코드에서 "ImageButton"컨트롤 ("using"부분에 있음).

당신이 그것을 자동으로 참조 그냥 뭔가 다른 생각

+0

감사합니다. 이것은 내가 놓친 것입니다. 같은 네임 스페이스 내에 모두 있기 때문에 내가 만든 컨트롤에 대한 참조를 추가해야한다는 것을 알지 못했습니다. 나는 그것을 Child Window에 추가 한 이후 코드 뒤에서 볼 수 있다고 가정했다. XAML에서 참조를 볼 수 있습니다. – PaulG

2

당신이 을 "목록이 컴파일되지 않는다"고 말했다 때.

내 생각 엔 당신이 당신의 파일의 목록 모음의 네임 스페이스를 포함 할 필요가 있다는 것입니다.

using System.Collections.Generic; 
+0

감사합니다. 내 메시지를 편집했습니다. – PaulG

1

을 추가합니다 "해결"코드에 "하여 ImageButton"을 마우스 오른쪽 단추로 클릭 만 할 수 있습니다 자동으로 해당 작업을 수행하려면, 당신은 왜 확실하지 않다 디스플레이에 (해당 childwindow에있는 경우) ImageButtons의 List <>을 유지하십시오. 그들이 모두 같은 컨테이너에 있다면 다음과 같은 것을 사용할 수 있습니다 :

// 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처럼 작동하여 선택한 항목을 호출자에게 전달합니다. 이 버튼 중 하나를 설정해야한다면 컨트롤 이름이 아닌 인덱스를 호출하기 만하면됩니다. 말이된다? – 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]); 
      } 
      ; 
관련 문제