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

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

问题描述

有没有办法,我能含住时数据源已被应用到它的绑定控件被通知的事件吗?

还是有另一种情况,其中我保证了数据源已被应用?



我用的WinForms(WPF自推出)工作,并使用带有数据绑定值的标签,以确定我的工作控件的类型我。许多控制可能具有相同的标记值,我必须检索,以执行业务逻辑与所需的标签控件。

问题是,我不知道什么时候执行我的搜索该变量的数值。我试图打电话后立即搜索变量值:

  myBindingSource.DataSource = OutputFunctions.Instance;
//是...我绑定到一个单身具有属性的列表。
//它不是最好的方法,但工程。

我的 Form.Load 事件处理中。但是,在搜索过程中,我见过的变量值未设置。这怎么可能,如果我刚刚设置数据源?

由于可以从内部管理code-背后对我的表格中可以看出,我已经正确通过设计师的属性窗口中设置的值:

  this.textBoxDTemp.DataBindings.Add(新System.Windows.Forms.Binding(
    标签,
    this.myBindingSource,
    KNOB_DRIVER_TEMP
    真正));

我已经采取了看 BindingComplete ,老实说,这看起来非常有前途的,只是它不绑定的初始化过程中触发,即使价值据说是从数据源到目标控制传播。

编辑:
每请求,数据源首先在内部$ C $设置C-背后的形式,例如:

  this.myBindingSource.DataSource = typeof运算(OutputFunctions);

和这里的情况下,单身它帮助。

 公共类OutputFunctions
{
    私有静态OutputFunctions实例;    公共静态OutputFunctions实例
    {
        得到
        {
            如果(例如== NULL)
            {
                例如=新OutputFunctions();
            }
            返回实例;
        }
    }    私人OutputFunctions(){}    公共字符串KNOB_DRIVER_TEMP {{返回KNOB_DRIVER_TEMP }}
    公共字符串KNOB_PASSENGER_TEMP {{返回KNOB_PASSENGER_TEMP }}
    公共字符串KNOB_FAN {{返回KNOB_FAN }}
}


解决方案

数据绑定表单load事件之前应该已经被激活。您所遇到的问题是因为由于对数据绑定的基础设施优化,结合直到他们成为第一次看到不发生隐形控制。这可能是因为WF的设计师们认为数据绑定将用于数据绑定属性只(如文本等),并没有任何意义做一个无形的控制。

如果你不害怕使用一些内部(或用户HighCore会说黑客),那么下面的帮助将有助于解决您的问题(我们使用的是类似的东西年):

 公共静态类ControlUtils
{
    静态只读动作<控制,布尔> CreateControlFunc =(动作<控制,布尔>)Delegate.CreateDelegate(typeof运算(动作<控制,布尔>)
        typeof运算(控制).GetMethod(CreateControl,BindingFlags.Instance | BindingFlags.NonPublic可,空,新的[] {typeof运算(布尔)},NULL));
    公共静态无效CreateControls(这种控制目标)
    {
        如果(!target.Created)
            CreateControlFunc(目标,真正的);
        其他
            的for(int i = 0; I< target.Controls.Count;我++)
                target.Controls [I] .CreateControls();
    }
}

,只是把你的窗体加载事件处理程序的开头

  this.CreateControls();

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

解决方案

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

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

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