我在winforms中有一个带有主窗体和userControl的windows c#app。
public partial class MainForm : Form
{
//myPanel is an instance of the user control
public Form1_Load(object sender, EventArgs e)
{
myCheckBox.DataBindings.Add("Checked", myPanel.buildParameters, "isChecked");
}
}
public partial class PublicationPanel : UserControl
{
public BuildParameters buildParameters = new BuildParameters ();
public PublicationPanel()
{
InitializeComponent();
buildParameters.isChecked= true;
buildParameters.Project = "";
buildParameters.Version = "";
ProjectList.DataBindings.Add("Text", buildParameters, "Project");
VersionText.DataBindings.Add("Text", buildParameters, "Version");
}
}
编辑:
public class BuildParameters : INotifyPropertyChanged
{
private bool _isChecked;
public bool isChecked
{
get { return _isChecked; }
set
{
_isChecked= value;
InvokePropertyChanged(new PropertyChangedEventArgs("isChecked"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, e);
}
}
问题是当表单加载时,复选框正确绑定。选中该复选框。但是当我取消选中该复选框并在我更改userControl中的字段值(projetList或versionText迷失焦点)后,再次检查checkox。
userControl中的绑定效果很好。
了解会发生什么?
非常感谢!