WinForms:响应正在应用的 BindingSource [英] WinForms: Respond to BindingSource being applied

查看:26
本文介绍了WinForms:响应正在应用的 BindingSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数据源被应用到其绑定控件时,是否有我可以锁定的事件?

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

或者是否有另一个事件,我可以保证数据源已被应用?<小时>我正在使用 WinForms(来自 WPF)并使用带有数据绑定值的标签来确定我正在使用的控件类型.许多控件可能具有相同的标记值,我必须检索具有所需标记的控件才能执行业务逻辑.

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.

在我的 Form.Load 事件处理程序中.但是,在搜索过程中,我看到未设置标签值.如果我刚刚设置了数据源,怎么会这样?

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

我查看了 BindingComplete,老实说,它看起来非常有前途,只是它不会在绑定初始化期间触发,即使该值据称是从数据中传播的-源到目标控件.

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.

根据请求,首先在表单的内部代码隐藏中设置数据源,如下所示:

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

推荐答案

数据绑定应该在你的表单加载事件之前已经被激活.您遇到的问题是由于数据绑定基础结构优化,不可见控件在第一次变得可见之前不会发生绑定.这可能是因为 WF 的设计者认为数据绑定将仅用于绑定数据属性(如 Text 等),而对于不可见的控件来说这样做没有意义.

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.

如果您不害怕使用一些内部组件(或者像用户 HighCore 所说的 hacks),那么以下帮助程序将帮助您解决问题(我们使用类似的东西已有多年):

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

这篇关于WinForms:响应正在应用的 BindingSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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