使控件集合只读
Making a collection of controls readonly
我目前正在使用 Devexpress
控件开发 Web 表单应用程序。我使用的控件是(但不限于):
- BootstrapComboBox
- BootstrapSpinEdit
- BootstrapTextBox
表单相当大,所以我想要实现的是通过例如IEnumerable<T>
然后遍历集合并将 ReadOnly
属性 设置为 true
。我知道我一次可以做一个控件,但我有超过 50 个控件,所以我想知道是否有更通用的方法。
我有以下片段:
public static void MakeControlReadOnly<T>(this IEnumerable<T> controlCollection)
{
foreach(var c in controlCollection)
c.ReadOnly = true;
}
但我一直收到错误消息:
Severity Code Description Project File Line Suppression State
Error CS1061 'T' does not contain a definition for 'ReadOnly' and no extension method 'ReadOnly' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?)
我明白这个错误,但我不确定如何克服它。
尝试将隐式类型 T
作为 ASPxEditBase
,DevExpress
的基本可编辑控件
public static void MakeControlReadOnly<T>(this IEnumerable<T> controlCollection)
where T: ASPxEditBase
{
controlCollection.ToList().ForEach(x=> x.ReadOnly = true);
}
您可以对包含只读 属性
的基 class 的泛型设置类型约束
public static void MakeControlReadOnly<T>(this IEnumerable<T> controlCollection) where T: BaseType //Set this to the correct base type
{
foreach(var c in controlCollection)
c.ReadOnly = true;
}
我目前正在使用 Devexpress
控件开发 Web 表单应用程序。我使用的控件是(但不限于):
- BootstrapComboBox
- BootstrapSpinEdit
- BootstrapTextBox
表单相当大,所以我想要实现的是通过例如IEnumerable<T>
然后遍历集合并将 ReadOnly
属性 设置为 true
。我知道我一次可以做一个控件,但我有超过 50 个控件,所以我想知道是否有更通用的方法。
我有以下片段:
public static void MakeControlReadOnly<T>(this IEnumerable<T> controlCollection)
{
foreach(var c in controlCollection)
c.ReadOnly = true;
}
但我一直收到错误消息:
Severity Code Description Project File Line Suppression State Error CS1061 'T' does not contain a definition for 'ReadOnly' and no extension method 'ReadOnly' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?)
我明白这个错误,但我不确定如何克服它。
尝试将隐式类型 T
作为 ASPxEditBase
,DevExpress
public static void MakeControlReadOnly<T>(this IEnumerable<T> controlCollection)
where T: ASPxEditBase
{
controlCollection.ToList().ForEach(x=> x.ReadOnly = true);
}
您可以对包含只读 属性
的基 class 的泛型设置类型约束public static void MakeControlReadOnly<T>(this IEnumerable<T> controlCollection) where T: BaseType //Set this to the correct base type
{
foreach(var c in controlCollection)
c.ReadOnly = true;
}