【发布时间】:2017-07-17 17:49:56
【问题描述】:
我将 UserControl 的实例作为 this 传递给继承 Page Class 和 Itemplate Interface 的类的构造函数,我正在保存 userControl 类型的实例,现在我如何访问用户 Control 的方法。 例如,MyUserControl 文件名为 uc_test 和 Class datagridtemplate:Page, Itemplate
我的 UserControl 有部分类名 uc_test 并且有方法
public int addtwonumbers()
{
return 10+20;
}
datadatagridtemplate x = new datagridtemplate(this); //new instance
在datagridtemplate.cs文件中:
public datagridtemplate(UserControl uc){}
在datagridtemplate类的一种方法中我想做以下
int sum = uc.addtwonumbers();
现在我想访问 uc_test 中定义的所有方法和属性,我该如何实现呢?
【问题讨论】:
-
在构造函数中将用户控件转换为您的类型:
uc = uc as MyType -
这里 Mytype 是你指的类文件?即 uc_test ?
-
如果你提供一个最小的例子,每个人都会更清楚:stackoverflow.com/help/mcve
-
我想是的,从您在此处发布的内容无法判断。无论您将类定义为什么,即
public class MyType : UserControl -
它说可以找到类型或命名空间(缺少指令或程序集引用),
标签: c# user-controls itemplate