C# NumericUpDown类型的问题
当我这样做的时候:C# NumericUpDown类型的问题,c#,.net,winforms,controls,numericupdown,C#,.net,Winforms,Controls,Numericupdown,当我这样做的时候: public static void BindData<T>(this System.Windows.Forms.Control.ControlCollection controls, T bind) { foreach (Control control in controls) { if (control.GetType() == typeof(System.Windows.Forms.TextBo
public static void BindData<T>(this System.Windows.Forms.Control.ControlCollection controls, T bind)
{
foreach (Control control in controls)
{
if (control.GetType() == typeof(System.Windows.Forms.TextBox) || control.GetType().IsSubclassOf(typeof(System.Windows.Forms.TextBox)))
{
UtilityBindData(control, bind);
}
else
{
if (control.Controls.Count == 0)
{
UtilityBindData(control, bind);
}
else
{
control.Controls.BindData(bind);
}
}
}
}
private static void UtilityBindData<T>(Control control, T bind)
{
Type type = control.GetType();
PropertyInfo propertyInfo = type.GetProperty("BindingProperty");
if (propertyInfo == null)
propertyInfo = type.GetProperty("Tag");
// rest of the code....
公共静态void BindData(this System.Windows.Forms.Control.ControlCollection控件,T bind)
{
foreach(控件中的控件)
{
if(control.GetType()==typeof(System.Windows.Forms.TextBox)| control.GetType().IsSubclassOf(typeof(System.Windows.Forms.TextBox)))
{
UtilityBindData(控件、绑定);
}
其他的
{
if(control.Controls.Count==0)
{
UtilityBindData(控件、绑定);
}
其他的
{
Controls.Controls.BindData(bind);
}
}
}
}
私有静态void UtilityBindData(控件控件,T绑定)
{
Type=control.GetType();
PropertyInfo PropertyInfo=type.GetProperty(“BindingProperty”);
如果(propertyInfo==null)
propertyInfo=type.GetProperty(“标记”);
//代码的其余部分。。。。
其中控件是System.Windows.Forms.Control.ControlCollection,在作为参数传递给此代码段的窗体上的控件中,有数字Updown,我在控件集合中找不到它们(controls=myForm.controls),但还有其他类型的控件(updownbutton,updownedit)。问题是我想获取NumericUpDown的Tag属性,但在使用检查表单控件的递归方法时无法获取该属性。由控件定义
因此,您根本不需要反思;您可以简单地编写
object tag = control.Tag;
由于控件的实际类型(例如,NumericUpDown
)未定义单独的标记
属性,并且GetProperty
未搜索基类属性,因此代码无法工作
顺便说一下,在您的第一个语句中,如果的意思是
if (control is TextBox)
是的,但仍然是控件。标记为null。这意味着您没有将其设置为任何值。我将位于表单上的numericupdown控件的Tag属性设置为某个值。但是在循环中没有numericupdown控件。而是有UpDownButton和UpDownEdit。感谢您的第一个“如果”)oops..阅读您编辑的答案..现在将检查其他内容..感谢您的帮助!添加了以下内容:if(control.Controls.Count==0 | |(control is NumericUpDown)){UtilityBindData(control,bind);}else{control.Controls.BindData(bind);}证明NumericUpDown是一个复杂的控件,它由几个其他的基本控件组成。