Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 guaranteed that the data-source has been applied?


I'm working with WinForms (coming from WPF) and am using tags with data-bound values in order to determine the type of control I'm working with. Many controls could have the same tag value, and I must retrieve the controls with the desired tag in order to perform business logic.

The problem is that I do not know when to perform my search for the tag values. I've attempted to search for the tag values immediately after calling:

myBindingSource.DataSource = OutputFunctions.Instance;
//Yes... I'm binding to a singleton with a list of properties.
//Its not the best method, but works.

inside my Form.Load event handler. But, during the search, I've seen that the tag values are not set. How can this be if I've just set the data source?

As can be seen from the internally managed code-behind for my form, I have properly set the value through the designer's Property window:

this.textBoxDTemp.DataBindings.Add(new System.Windows.Forms.Binding(
    "Tag",
    this.myBindingSource,
    "KNOB_DRIVER_TEMP",
    true));

I've taken a look at the BindingComplete, which honestly looks very promising, except that it doesn't trigger during the initialization of the binding, even though the value supposedly is propagating from the data-source to the target control.

EDIT: Per requested, the data-source is first set in the internal code-behind for the form as such:

this.myBindingSource.DataSource = typeof(OutputFunctions);

And here is the singleton in case it helps.

public class OutputFunctions
{
    private static OutputFunctions instance;

    public static OutputFunctions Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new OutputFunctions();
            }
            return instance;
        }
    }

    private OutputFunctions() { }

    public string KNOB_DRIVER_TEMP { get { return "KNOB_DRIVER_TEMP"; } }
    public string KNOB_PASSENGER_TEMP { get { return "KNOB_PASSENGER_TEMP"; } }
    public string KNOB_FAN { get { return "KNOB_FAN"; } }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
305 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...