C# 模式为DataSourceUpdateMode.OnPropertyChanged时的Winforms绑定和验证
我试图在WinForms应用程序中实现验证和绑定 我有一个版本表单,其中多个属性绑定到同一个对象,实现了INotifyPropertyChanged,并且我的所有绑定都设置为DataSourceUpdateMode.OnPropertyChanged 我的情况是,当我编辑某些值时,其他一些值可能会相应地更改:C# 模式为DataSourceUpdateMode.OnPropertyChanged时的Winforms绑定和验证,c#,winforms,validation,binding,C#,Winforms,Validation,Binding,我试图在WinForms应用程序中实现验证和绑定 我有一个版本表单,其中多个属性绑定到同一个对象,实现了INotifyPropertyChanged,并且我的所有绑定都设置为DataSourceUpdateMode.OnPropertyChanged 我的情况是,当我编辑某些值时,其他一些值可能会相应地更改: public class BindingRecord : INotifyPropertyChanged { #region INotify stufff - standard!!!
public class BindingRecord : INotifyPropertyChanged {
#region INotify stufff - standard!!!
{......}
#endregion
private string p1;
private string p2;
public BindingRecord () { }
[Required(ErrorMessage = "Required Prop1")]
public string Prop1 {
get { return p1; }
set {
p1 = value;
RaisePropertyChanged("Prop1");
}
}
[Required(ErrorMessage = "Required Prop2")]
public string Prop2 {
get { return p2; }
set {
p2 = value;
if (!string.IsNullOrEmpty(p2) && [...some condition linking p1 & p2...]) {
//!!!!! NOTE I use the
// PROPERTY => this raises the Notify event !!!!!
Prop1 = "some value!!!";
}
RaisePropertyChanged("Prop2");
}
}
}
在我的表单中,有两个编辑框以OnPropertyChanged模式绑定到Prop1和Prop2
private void Form1_Load(object sender, EventArgs e) {
// Empty binding re
BindingRecord rec = new BindingRecord();
rec.Prop1 = "";
rec.Prop2 = "";
//
txtEdit1.DataBindings.Add("Text", rec, "Prop1", true, DataSourceUpdateMode.OnPropertyChanged);
txtEdit2.DataBindings.Add("Text", rec, "Prop2", true, DataSourceUpdateMode.OnPropertyChanged);
// validate children!!
this.ValidateChildren();
}
另外,为了让用户清楚哪些属性是必需的,我在输入编辑表单=>后调用ValidateChildren,最初,Prop1和Prop2都是emtpy,因此带有验证错误
问题是,当我编辑Prop2时(甚至在验证之前),我设置了Prop1,因为我的绑定是OnPropertyChange,这也会更新绑定的编辑框,正确显示“some value!!!”文本。。。。但是验证错误仍然存在
所以问题是:如何确保在属性更改时调用控件的验证,以便在编辑Prop2时清除Prop1的验证错误
提前谢谢!
Nik尝试在
Prop1
中手动引发验证事件。未提出对Prop1
的验证,因为txtEdit1
没有关注它。请您澄清我将如何验证?注意:我没有访问Prop2中txtEdit1/2的权限。。。