【问题标题】:How to create an array of User Controls / indexing user controls in C# / Silverlight?如何在 C#/Silverlight 中创建用户控件数组/索引用户控件?
【发布时间】:2011-06-03 14:30:13
【问题描述】:

我以前是 VB 控件数组的老用户。现在已经没有了,所以我正在寻找替代品。

我在 C# 中为 Silverlight 创建了一个用户控件。该控件公开了一个布尔属性 IsChecked。

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

在子窗口中,我填充了一系列这些控件并将它们用作 RadioButtons 的等效项,即,当我单击一个时,任何其他具有 IsChecked = true 的控件都将设置为 false,我想到了做类似的事情:

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

然后,要清除他们所有的 IsChecked,但我需要的那个(由一些“索引”变量指向),我会做类似的事情:

foreach (ImageButton imgbutton in imgButtons) imgbutton.IsChecked = false;
imgbuttons[index].IsChecked = true;

我遇到的问题是List&lt;ImageButton&gt; 没有编译。我在收藏方面非常不稳定,无法弄清楚我错过了什么。我可以为标准控件做,但不允许我在那里获得用户控件。

谢谢!

PS:我想只自定义一个 RadioButton 控件,但没有 Blend,我正在将此图像控件用于其他类型的控件。但是,如果您认为有更好的方法来实现这一点,请告诉我。

编辑:编译器说“找不到类型或命名空间名称'ImageButton'(您是否缺少 using 指令或程序集引用?)”

【问题讨论】:

    标签: c# silverlight arrays user-controls controls


    【解决方案1】:

    确保您在代码中引用了“ImageButton”控件(在“使用”部分)。

    要自动执行此操作,您只需右键单击代码中的“ImageButton”并单击“Resolve”,它将自动添加引用

    【讨论】:

    • 谢谢,这就是我所缺少的。因为它都驻留在同一个命名空间中,所以我不知道我必须添加对我创建的控件的引用。我假设由于我将它添加到子窗口中,它在后面的代码中也是可见的。我可以在 XAML 中看到引用。
    【解决方案2】:

    当您说“列表未编译”时,您没有告诉我们编译器错误是什么

    我的猜测是您需要在文件中包含 List 集合的命名空间。

    using System.Collections.Generic;
    

    【讨论】:

      【解决方案3】:

      只是想到了别的东西,我不确定如果它们在显示中(在那个子窗口中),你为什么要保留一个 List 的 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;
      

      【讨论】:

      • 我保留了一个用于索引目的的列表。所有这些图像按钮都像 RadioButton 一样,将所选项目传递给它的调用者。如果我需要设置这些按钮之一,我只需调用它的索引而不是控件名称。有意义吗?
      • 那么您可以使用 grd.Children.IndexOf(imgBtn); 获取子对象的实际索引。使用索引检索相同的项目:grd.Children[i].
      • 这也是一个有趣的解决方案。只是我必须在调用者的索引和 grd.Children.IndexOf(imgButton) 之间创建一个映射。
      【解决方案4】:

      这可能会有所帮助。

      用户控制代码:

      //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]);
                  }
                  ;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-07
        • 2023-03-30
        相关资源
        最近更新 更多