开发者

Windows Forms Binding: is there an event similar to DataBindingComplete, but fired when ALL bindings are completed?

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.

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.

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);
    }

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).

Is there a better way?

(yes, I'm trying to adopt MVVM in a Windows Forms project, and I've added a bindable "SelectedItems" prope开发者_如何学编程rty to the DataGridView...)


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.

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...
    }
}

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜