【问题标题】:How to Read the Data Binding on a Windows Forms Controls如何读取 Windows 窗体控件上的数据绑定
【发布时间】:2018-12-05 15:49:22
【问题描述】:

我知道如何对控件进行数据绑定。

在代码中读取数据绑定设置的可接受方式是什么?

在 GUI 中,有一个带有 TagText(DataBindings) 部分。

在下面,您可以看到 (DataBindings) 具有 Text: _rosterBS - LastName

我正在尝试获取此绑定源和属性(即读取文本值)。

在设计器中,

// 
// txtLastName
// 
this.txtLastName.DataBindings.Add(new System.Windows.Forms.Binding("Text", this._rosterBS, "LastName", true));
this.txtLastName.Location = new System.Drawing.Point(7, 32);
this.txtLastName.Name = "txtLastName";
this.txtLastName.Size = new System.Drawing.Size(198, 20);
this.txtLastName.TabIndex = 1;

DataBindings 来自这个版本的 Control 类:

//
// Summary:
//     Defines the base class for controls, which are components with visual representation.
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ComVisible(true)]
[DefaultEvent("Click")]
[DefaultProperty("Text")]
[Designer("System.Windows.Forms.Design.ControlDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[DesignerSerializer("System.Windows.Forms.Design.ControlCodeDomSerializer, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[ToolboxItemFilter("System.Windows.Forms")]
public class Control : Component, IDropTarget, ISynchronizeInvoke, IWin32Window, IArrangedElement, IBindableComponent, IComponent, IDisposable
{

从那里,我开始追查IBindableComponent

//
// Summary:
//     Enables a non-control component to emulate the data-binding behavior of a Windows
//     Forms control.
public interface IBindableComponent : IComponent, IDisposable
{
    //
    // Summary:
    //     Gets or sets the collection of currency managers for the System.Windows.Forms.IBindableComponent.
    //
    // Returns:
    //     The collection of System.Windows.Forms.BindingManagerBase objects for this System.Windows.Forms.IBindableComponent.
    BindingContext BindingContext { get; set; }

然后我去追查BindingContext,但看起来我没有得到任何有我想要的信息的东西。

仅供参考:我为什么要这个?目前,我可以检查 Linq-To-SQL 数据上下文的更改以提示退出时保存,但这不会让用户知道更改了哪些字段。我有一种方法可以手动比较每个控件中的原始版本和新版本,但是在我们的整个解决方案套件中实现这一点需要手动编码每个表单。

更新:这不适用于 DevExpress 控件。

【问题讨论】:

  • 控件的 DataBindings 有一个属性 BindingMemberInfo。例如.DataBindings["Text"].BindingMemberInfo.BindingField 将获取您绑定到此控件的字段名称。
  • @GuidoG,原来这个BindingField 是记录的主键。我正在寻找一种让代码读取绑定到控件的字段/列的方法。

标签: c# winforms data-binding


【解决方案1】:

暂时,这是我想出的一些东西:

// Occurs when all the clients have been bound to the Windows Forms BindingSource
private void rosterBindingSource_BindingComplete(object sender, BindingCompleteEventArgs e)
{
    if (_dataDictionary != null)
    {
        _dataDictionary.Clear();
    }
    _dataDictionary = new Dictionary<string, Control>();
    rosterBindingSource_BindingData(this);
}

private void rosterBindingSource_BindingData(Control control)
{
    foreach (var item in control.Controls)
    {
        var ctrl = item as Control;
        if (ctrl != null)
        {
            rosterBindingSource_BindingData(ctrl);
            // see what is data bound
            var bindable = item as IBindableComponent;
            if (bindable != null)
            {
                foreach (Binding binding in bindable.DataBindings)
                {
                    if (!_dataDictionary.ContainsKey(binding.PropertyName))
                    {
                        _dataDictionary.Add(binding.PropertyName, ctrl);
                    }
                }
            }
        }
    }
}

这可能是最好的方法。不过,我仍然需要使绑定源更通用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    相关资源
    最近更新 更多