没有 OnLoad 方法的 System.Windows.Controls.Control

发布于 2024-12-08 19:14:13 字数 756 浏览 0 评论 0原文

我需要动态修改一些数据绑定。因此,我计划在其父级控件初始化期间/之后执行该操作。

但尽管 有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

软的没边 2024-12-15 19:14:13

这是 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.

柠栀 2024-12-15 19:14:13

您确定您是从 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 from System.Windows.Forms.Control?

System.Windows.Forms.Control does not provide a virtual OnLoad method.

水中月 2024-12-15 19:14:13

System.Windows.Controls.Control 不提供 OnLoad 方法,请参阅MSDN

System.Windows.Controls.Control doesn't provider OnLoad method, see MSDN

待天淡蓝洁白时 2024-12-15 19:14:13

根据您的评论,我建议您创建 ViewBinder ,它可以轻松设置转换器并具有最大的透明度。

查看 Rob Eisenberg 的演讲 MIX10 和 Caliburn 或可从该页面下载的演讲代码。

根据约定,框架定位 UI 元素并将其与同名属性进行匹配。并自动创建和调整绑定:

private static void BindProperties(FrameworkElement view, IEnumerable<PropertyInfo> properties)
{
  foreach (var property in properties)
  {
    var foundControl = view.FindName(property.Name) as DependencyObject;
    if(foundControl == null) // find the element
      continue;

    DependencyProperty boundProperty;
    if(!_boundProperties.TryGetValue(foundControl.GetType(), out boundProperty))
      continue;
    if(((FrameworkElement)foundControl).GetBindingExpression(boundProperty) != null) // already bound
      continue;

    var binding = new Binding(property.Name) // create the binding
    {
      Mode = property.CanWrite ? BindingMode.TwoWay : BindingMode.OneWay,
      ValidatesOnDataErrors = Attribute.GetCustomAttributes(property, typeof(ValidationAttribute), true).Any()
    };

    if (boundProperty == UIElement.VisibilityProperty && typeof(bool).IsAssignableFrom(property.PropertyType))
      binding.Converter = _booleanToVisibilityConverter;

    BindingOperations.SetBinding(foundControl, boundProperty, binding);
  }
}

绑定是在 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:

private static void BindProperties(FrameworkElement view, IEnumerable<PropertyInfo> properties)
{
  foreach (var property in properties)
  {
    var foundControl = view.FindName(property.Name) as DependencyObject;
    if(foundControl == null) // find the element
      continue;

    DependencyProperty boundProperty;
    if(!_boundProperties.TryGetValue(foundControl.GetType(), out boundProperty))
      continue;
    if(((FrameworkElement)foundControl).GetBindingExpression(boundProperty) != null) // already bound
      continue;

    var binding = new Binding(property.Name) // create the binding
    {
      Mode = property.CanWrite ? BindingMode.TwoWay : BindingMode.OneWay,
      ValidatesOnDataErrors = Attribute.GetCustomAttributes(property, typeof(ValidationAttribute), true).Any()
    };

    if (boundProperty == UIElement.VisibilityProperty && typeof(bool).IsAssignableFrom(property.PropertyType))
      binding.Converter = _booleanToVisibilityConverter;

    BindingOperations.SetBinding(foundControl, boundProperty, binding);
  }
}

Binding is done explicitly in public static void Bind(object viewModel, DependencyObject view) method, which takes all properties defined in viewModel type and binds them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文