WinForms数据绑定到自定义属性会引发异常

时间:2014-10-10 19:23:15

标签: c# winforms data-binding

如果这是重复的话我很抱歉,但我在网上搜索并没有找到答案

我正在尝试绑定控件Enabled(或ReadOnly TextBox):

this.tbProj.DataBindings.Add(new Binding("Enabled", this, "CanEdit", false, DataSourceUpdateMode.OnPropertyChanged, false));
this.btnSave.DataBindings.Add(new Binding("Enabled", this, "Dirty", false, DataSourceUpdateMode.OnPropertyChanged, false));

以下属性:

public bool Dirty { get; set; }

private bool CanEdit
{
    get { return this._CurrentRecord.CanEdit(); }
}

我在尝试System.ArgumentException: Cannot bind to the property or column CanEdit on the DataSource.表单时获得ShowDialog()

如果我将EnabledReadOnly绑定到Dirty,那么一切都很好。我尝试了get { return true; },甚至添加了一个setter:set { bool bummy=value; },同样的错误。我甚至将CanEdit更改为自动实现的属性,与Dirty(声明中仅get; set;相同)无效...

请帮忙。

感谢任何提示和建议。

1 个答案:

答案 0 :(得分:1)

该物业必须公开:

public bool CanEdit
{
    get { return this._CurrentRecord.CanEdit(); }
}