没有 OnLoad 方法的 System.Windows.Controls.Control
我需要动态修改一些数据绑定。因此,我计划在其父级控件初始化期间/之后执行该操作。
但尽管 有关 Control.OnLoad Method 的 msdn 页面,我的类拒绝编译:
错误 810“Views.Test.OnLoad(System.EventArgs)”:找不到合适的方法来覆盖
我的代码:
class Test : Control
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (true)
{
System.Diagnostics.Debug.Assert(false);
}
}
}
知道我做错了什么吗?
编辑: @Roken 注意到我与 System.Web.UI.Control 不匹配,因为我的类派生自 System.Windows.Controls.Control
所以我的问题变成:我应该何时以及如何执行我的操作对此控件的绑定进行修改?要重写什么方法,或者要订阅什么事件?
I need to modify some data bindings dynamically. So I had planned to perform the operation during/after the initialisation of the control in its parent.
But despite the msdn page on Control.OnLoad Method, my class refuses to compile:
Error 810 'Views.Test.OnLoad(System.EventArgs)': no suitable method found to override
My code:
class Test : Control
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (true)
{
System.Diagnostics.Debug.Assert(false);
}
}
}
Any idea about what I am doing wrong ?
Edit: @Roken has noticed that I was mismatching with the System.Web.UI.Control because my class derives from System.Windows.Controls.Control
So my question becomes: When and how should I perform my modifications on the binding of this control ? What method to override, or what event to subscribe to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是 Windows 窗体控件还是 Web 控件?您提供的链接用于网页控制; WinForms 控件不包含 OnLoad()。对于 WinForms,OnCreateControl() 可能有用,对于 WPF,OnInitialized() 可能有用。
Is this a Windows Forms control or a Web control? The link you provided is for the web control; the WinForms Control does not contain OnLoad(). OnCreateControl() might be of use to you for WinForms, or OnInitialized() for WPF.
您确定您是从
System.Web.UI.Control
派生的,而不是从System.Windows.Forms.Control
派生的吗?System.Windows.Forms.Control
不提供虚拟OnLoad
方法。Are you sure you derive from
System.Web.UI.Control
and not fromSystem.Windows.Forms.Control
?System.Windows.Forms.Control
does not provide a virtualOnLoad
method.System.Windows.Controls.Control
不提供OnLoad
方法,请参阅MSDNSystem.Windows.Controls.Control
doesn't providerOnLoad
method, see MSDN根据您的评论,我建议您创建
ViewBinder
,它可以轻松设置转换器并具有最大的透明度。查看 Rob Eisenberg 的演讲 MIX10 和 Caliburn 或可从该页面下载的演讲代码。
根据约定,框架定位 UI 元素并将其与同名属性进行匹配。并自动创建和调整绑定:
绑定是在
public static void Bind(object viewModel, DependencyObject view)
方法中显式完成的,该方法采用viewModel
类型中定义的所有属性并进行绑定他们。Based on your comment I recommend you to create
ViewBinder
that will set up the converters with a little effort and with maximized transparency.Check the Rob Eisenberg's speak at MIX10 and Caliburn or the code from the speak that is downloadable from that page.
Based on conventions the framework locates UI element and matches it with property of the same name. And creates and tweaks the binding automatically:
Binding is done explicitly in
public static void Bind(object viewModel, DependencyObject view)
method, which takes all properties defined inviewModel
type and binds them.