Windows窗体绑定:是否有类似于DataBindingComplete的事件,但是当所有绑定完成时都会被触发? [英] Windows Forms Binding: is there an event similar to DataBindingComplete, but fired when ALL bindings are completed?

查看:382
本文介绍了Windows窗体绑定:是否有类似于DataBindingComplete的事件,但是当所有绑定完成时都会被触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有当所有的初始数据绑定完成后,我才需要更改某个DataGridView的属性(一个DataSourceUpdateMode)。

I need to change a certain DataGridView's property (a DataSourceUpdateMode for one of its binding) only when ALL of its initial data bindings are completed.

我尝试订阅DataBindingComplete事件,但它被触发了太多次(每个绑定关联到控件的一个或多个时间);我需要的是一个更全面的AllDataBindingsComplete事件,当控件准备向用户显示时触发。

I tried subscribing to the "DataBindingComplete" event, but it's fired too many times (one or more time for each binding associated to the control); what I need is a more global "AllDataBindingsComplete" event, fired when the control is ready to be displayed to the user.

作为临时解决方法,我使用MouseDown事件(我已经假设当用户能够点击控件,这意味着控件显示... :)和我正在玩的事件 - SelectionChanged - 在MouseDown后被触发):

As a temporary workaround, I'm using the MouseDown event (I've assumed that when the user is able to click the control, it means that the control is displayed... :) and the events I'm playing with - SelectionChanged - are fired after the MouseDown):

    protected override void OnMouseDown(MouseEventArgs e)
    {
        Binding selectedItemsBinding = this.DataBindings["SelectedItems"];
        if (selectedItemsBinding != null)
        {
            selectedItemsBinding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
        }

        base.OnMouseDown(e);
    }

它的作品,但它闻起来像一个丑陋的黑客A LOT(和它被称为太多次了,只有一次足够满足我的需要)。

It works, but it smells like an ugly hack A LOT (and it's called too many times, only one time is enough for my needs).

有更好的方法吗?

(是的,我试图在Windows窗体项目中采用MVVM,我已经添加了一个可绑定的SelectedItems属性到DataGridView ...)

(yes, I'm trying to adopt MVVM in a Windows Forms project, and I've added a bindable "SelectedItems" property to the DataGridView...)

推荐答案

我在 Windows窗体中所做的工作形式级别,并且可以根据您想要的控件进行即兴创建,就是将Windows窗体基类子类分为我自己的。然后,在其构造函数中,附加一个额外的事件调用到 Load()事件。

What I've done at the Windows Forms form level, and may be improvised down to just the control(s) you want, is to subclass the Windows Forms baseclass into my own. Then, in its constructor, attach an extra event call to the Load() event.

em> else 是完全加载,只有这样才能打击我的自定义方法(子类)。因为它是调用堆栈链的底部附加到事件队列,我知道它是最后一切,其他所有的完成...这是一个概念的代码片段。

So when everything else is completely loaded, only THEN will it hit my custom method (of the subclass). Since it is the bottom of the call-stack chain being attached to the event queue, I know it's last and everything else is done... Here's a snippet of the concept.

public class MyForm : Form
{
    public MyForm()
    {
        this.Load += AfterEverythingElseLoaded;
    }

    private void AfterEverythingElseLoaded(object sender, EventArgs e)
    {
        // Do my own things here...
    }
}

这个概念可以应用于 Init()函数,如果这更适合你的控制...让其他内容得到initialized(),然后你的AfterInitialized()函数。

This concept can be applied to the Init() function too if that's more appropriate for your control... Let everything else within it get initialized(), then do you the "AfterInitialized()" function.

这篇关于Windows窗体绑定:是否有类似于DataBindingComplete的事件,但是当所有绑定完成时都会被触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆