WinForms: Respond to BindingSource being applied

前端 未结 2 948
眼角桃花
眼角桃花 2020-12-11 07:39

Is there an event that I can latch onto to be notified when the datasource has been applied to its bound controls?

Or is there another event, in which I am guarantee

相关标签:
2条回答
  • 2020-12-11 08:28

    I think you are looking for the control's

    "BindingContextChanged" event.

    Are you trying to force add some hook to the binding when it occurs?.

    Since you are looking AFTER the entire form is prepared and bindings are established, you can probably hook to the "LOAD" event. The form prepares everything first, then will call the "Load" event. If anything is subscribed (listening) to it, they will be notified. Once that is invoked, you can run and cycle through all controls on the form and look for whatever part / component / tag / control type, etc.

        public Form1()
        {
            InitializeComponent();
    
            this.VisibleChanged += Form1_VisibleChanged;
        }
    
        void Form1_VisibleChanged(object sender, EventArgs e)
        {
            if (!this.Visible)
                return;
    
            // Disable the event hook, we only need it once.
            this.VisibleChanged -= Form1_VisibleChanged;
    
            StringBuilder sb = new StringBuilder();
            foreach (Control c in this.Controls)
                sb.AppendLine(c.Name);
        }
    

    Edit per comment. I changed from the LOAD event to the VISIBILITY event. At this point, the form is now being shown so all your stuff SHOULD be completed and available. So, the initial check is to make sure it IS becoming visible. If so, immediately remove itself from the event handler, you only need it done once and not every possible time it gets shown / hidden / shown ...

    0 讨论(0)
  • 2020-12-11 08:29

    Data binding should already been activated before your form load event. The problem you are experiencing is because due to to data binding infrastructure optimization, binding does not happen for invisible controls until they become visible for a first time. This is probably because the designers of WF were thinking that the data binding will be used to bind data properties only (like Text etc.) and doesn't make sense to do that for an invisible controls.

    If you are not afraid to use some internals (or as user HighCore would say hacks), then the following helper would help solving your problem (we are using something similar for a years):

    public static class ControlUtils
    {
        static readonly Action<Control, bool> CreateControlFunc = (Action<Control, bool>)Delegate.CreateDelegate(typeof(Action<Control, bool>),
            typeof(Control).GetMethod("CreateControl", BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(bool) }, null));
        public static void CreateControls(this Control target)
        {
            if (!target.Created)
                CreateControlFunc(target, true);
            else
                for (int i = 0; i < target.Controls.Count; i++)
                    target.Controls[i].CreateControls();
        }
    }
    

    and just put at the beginning of your form load event handler

    this.CreateControls();
    
    0 讨论(0)
提交回复
热议问题