【问题标题】:Binding the Checkbox.Checked property to property on DataSet将 Checkbox.Checked 属性绑定到 DataSet 上的属性
【发布时间】:2011-05-17 21:19:13
【问题描述】:

环境: Visual Studio 2010、.NET 4.0、WinForms

我有一个实现INotifyPropertyChangedDataSet,并在DataSet 上创建了一个bool 属性。我正在尝试将 CheckBox.Checked 属性绑定到该 bool 属性。当我尝试在设计器中执行此操作时,我看到 DataSetDataSet 中的表格,但看不到属性。我尝试手动执行此操作,但收到未找到该属性的错误。我看到我正在做的唯一不同的事情是表单上的属性是正在实例化的DataSet 的超类,但我什至看不出这会如何影响任何事情。下面是一个代码 sn-p。

派生类定义

public class DerivedDataSetClass: SuperDataSetClass, INotifyPropertyChanged
{
  private bool _mainFile = false;
  public bool MainFile
  {
    get { return this._mainFile; }
    set { 
      this._mainFile = value;
      this.NotifyPropertyChanged("MainFile");
    }
  }
}

属性定义

private SuperDataSetClass _dataSet;
public DerivedDataSetClass DataSet
{
   get { return (DerivedDataSetClass)_dataSet;
}

导演

this._DataSet = new DerivedDataSetClass (this);

this.mainFileBindingSource = new BindingSource();
this.mainFileBindingSource.DataSource = typeof(DerivedDataSetClass);
this.mainFileBindingSource.DataMember = "MainFile";

var binding = new Binding("Checked", this.mainFileBindingSource, "MainFile");
this.chkMainFile.DataBindings.Add(binding);

想法?

【问题讨论】:

    标签: c# winforms data-binding


    【解决方案1】:

    问题直接来自您使用DerivedDataSetClass 的方式。因为它是DataSet,所以完成的任何绑定都将使用其默认的DataViewManager,它“推动”进一步绑定到Tables 绑定。

    当您绑定到 DerivedDataSet MainFile 属性时,在后台所做的是尝试绑定到数据集表中名为 MainFile 的表。当然这会失败,除非你在数据集中真的有这样的表。出于同样的原因,您不能绑定到基础DataSet任何其他属性 - 例如。 LocaleHasErrors - 它还检查此类表是否存在,而不是属性。

    解决这个问题的方法是什么?您可以尝试实施不同的DataViewManager - 但是我无法找到关于该主题的可靠资源。

    我的建议是为您的MainFile 属性和关联的DerivedDataSetClass 创建简单的包装类,如下所示:

    public class DerivedDataSetWrapper : INotifyPropertyChanged
    {
        private bool _mainFile;
    
        public DerivedDataSetWrapper(DerivedDataSetClass dataSet)
        {
            this.DataSet = dataSet;
        }
    
        // I assume no notification will be needed upon DataSet change;
        // hence auto-property here
        public DerivedDataSetClass DataSet { get; private set; }
    
        public bool MainFile
        {
            get { return this._mainFile; }
            set
            {
                this._mainFile = value;
                this.PropertyChanged(this, new PropertyChangedEventArgs("MainFile"));
            }
        }
    }
    

    现在您可以绑定到数据集内部内容(表格)以及包装类上的MainFile

    var wrapper = new DerivedDataSetWrapper(this._DataSet);
    BindingSource source = new BindingSource { DataSource = wrapper };
    
    // to bind to checkbox we essentially bind to Wrapper.MainFile
    checkBox.DataBindings.Add("Checked", source, "MainFile", false, 
       DataSourceUpdateMode.OnPropertyChanged);
    

    要绑定数据集中表中的数据,您需要绑定到DerivedDataSetWrapper DataSet 属性,然后浏览表名称和列。例如:

    textBox.DataBindings.Add("Text", source, "DataSet.Items.Name");
    

    ...将绑定到原始_DataSet 中的表Items 和列Name

    【讨论】:

    • 不幸的是,这没有帮助。当表单尝试显示时,我得到一个无效参数异常,即属性 MainFile 无法在 System.Forms 的 CheckBindings 方法中绑定。
    • @wraith808:是的,我重现了你的问题。它来自DataSet 绑定是如何完成的 - 检查我的编辑。
    • 感谢您的信息!我试图使用 CheckBox 单击方法来解决问题,但考虑到开销以及这是一个现有应用程序的事实,我想这更清洁。但我很高兴知道原因、解决方法,而且我并不为我所看到的而疯狂。 ;)
    猜你喜欢
    • 2011-02-20
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    相关资源
    最近更新 更多