【发布时间】:2018-12-05 15:49:22
【问题描述】:
我知道如何对控件进行数据绑定。
在代码中读取数据绑定设置的可接受方式是什么?
在 GUI 中,有一个带有 Tag 和 Text 的 (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