如何在C#/Silverlight中创建用户控件数组/索引用户控件?
我以前是VB控件数组的老用户。它现在不见了,所以我正在寻找替代品 我用C#为Silverlight创建了一个用户控件。此控件显示已检查的bool属性如何在C#/Silverlight中创建用户控件数组/索引用户控件?,c#,silverlight,arrays,user-controls,controls,C#,Silverlight,Arrays,User Controls,Controls,我以前是VB控件数组的老用户。它现在不见了,所以我正在寻找替代品 我用C#为Silverlight创建了一个用户控件。此控件显示已检查的bool属性 <ImageButton Name"imgbutton1" IsChecked="True"/> <ImageButton Name"imgbutton2" IsChecked="False"/> <ImageButton Name"imgbutton3" IsChecked="False"/> 我遇到的问题是
<ImageButton Name"imgbutton1" IsChecked="True"/>
<ImageButton Name"imgbutton2" IsChecked="False"/>
<ImageButton Name"imgbutton3" IsChecked="False"/>
我遇到的问题是,List
没有编译。我在收藏方面非常古怪,不知道我错过了什么。我可以使用标准控件,但不允许在其中使用用户控件
谢谢
PS:我想定制一个RadioButton控件,但没有Blend,我正在使用这个图像控件来实现其他类型的控件。然而,如果你认为有更好的方法来实现这一点,请让我知道
编辑:编译器说“找不到类型或命名空间名称‘ImageButton’(是否缺少using指令或程序集引用?)”当您说“列表未编译”时,您没有告诉我们编译器错误是什么 我猜您需要在文件中包含列表集合的名称空间
using System.Collections.Generic;
当您说“列表未编译”时,您没有告诉我们编译器错误是什么 我猜您需要在文件中包含列表集合的名称空间
using System.Collections.Generic;
确保在代码中(在“使用”部分)有对“ImageButton”控件的引用
要自动执行此操作,只需右键单击代码中的“ImageButton”并单击“Resolve”,它将自动添加引用确保代码中有对“ImageButton”控件的引用(在“using”部分)
要自动做到这一点,您只需右键单击代码中的“ImageButton”并单击“Resolve”,它将自动添加引用,我不知道您为什么要保留一个ImageButton列表,如果它们在显示中(在该子窗口中)。如果它们都在同一个容器中,您可以使用以下内容:
// 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;
//这里的“grd”是您的网格容器,但它可能是另一种类型的容器
foreach(grd.Children.OfType()中的ImageButton imgBtn)
imgBtn.IsChecked=false;
我刚刚想到了一些其他的东西,我不知道为什么您要保留一个ImageButton列表,如果它们在显示屏上(在该子窗口中)。如果它们都在同一个容器中,您可以使用如下内容:
// 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;
//这里的“grd”是您的网格容器,但它可能是另一种类型的容器
foreach(grd.Children.OfType()中的ImageButton imgBtn)
imgBtn.IsChecked=false;
这可能会有所帮助
用户控制代码:
//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;
}
}
现在是用户控件数组
你可以把它写在按钮上
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]);
}
;
this.SuspendLayout();
Random r=新随机数(DateTime.Now.毫秒);
对于(int i=0;i<3;i++)
{
userControl11[i]=新的UserControl1();
this.userControl11[i].formColor=Color.FromArgb(r.Next(255)、r.Next(255)、r.Next(255));
this.userControl11[i].Location=new System.Drawing.Point(133133*(i+1));
this.userControl11[i].Name=“userControl11”;
this.userControl11[i].Size=new System.Drawing.Size(278133);
this.userControl11[i].TabIndex=0;
this.Controls.Add(this.userControl11[i]);
}
;
这可能会有所帮助
用户控制代码:
//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;
}
}
现在是用户控件数组
你可以把它写在按钮上
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]);
}
;
this.SuspendLayout();
Random r=新随机数(DateTime.Now.毫秒);
对于(int i=0;i<3;i++)
{
userControl11[i]=新的UserControl1();
this.userControl11[i].formColor=Color.FromArgb(r.Next(255)、r.Next(255)、r.Next(255));
this.userControl11[i].Location=new System.Drawing.Point(133133*(i+1));
this.userControl11[i].Name=“userControl11”;
this.userControl11[i].Size=new System.Drawing.Size(278133);
this.userControl11[i].TabIndex=0;
this.Controls.Add(this.userControl11[i]);
}
;
谢谢,这就是我所缺少的。由于它位于同一命名空间中,我不知道我必须添加对我创建的控件的引用。我假设,自从我将它添加到子窗口后,它在代码隐藏中也可见。我可以在XAML中看到引用。谢谢,这就是我所缺少的。因为它位于同一名称空间,我不知道我必须添加对我创建的控件的引用。我假设自从我将其添加到子窗口后,它在代码隐藏中也可见。我可以在XAML中看到该引用。我保留一个列表用于索引目的。所有这些ImageButton都像RadioButton一样,将所选项目传递给其调用者。如果我需要要设置其中一个按钮,我只调用它的索引而不是控件名。有意义吗?您可以使用grd.Children.IndexOf(imgBtn);;获取子对象的实际索引。要使用索引检索同一项:grd.Children[I]。这也是一个有趣的解决方案。只是我必须在调用方的索引和grd.Children.IndexOf(imgButton)之间创建一个映射。我保留一个列表用于索引。所有这些ImageButton都像一个单选按钮一样,将所选项目传递给调用者。如果需要设置其中一个按钮,我只调用其索引,而不是通过控件名。有意义吗?您可以使用grd.Children.IndexOf(imgBtn)获取子对象的实际索引;。检索索引为grd.Children[i]的同一项。这也是一个有趣的解决方案。只是我必须在调用方的索引和grd.Children.IndexOf(imgButton)之间创建一个映射。