我有CheckBox
我希望控制表单中所有Enabled
的{{1}}属性。我想使用TextBoxes
执行此操作。有没有办法可以创建一个DataBinding
我可以用于表单中的每个DataBinding
?
TextBox
答案 0 :(得分:4)
为什么不分别创建一个新的Binding? :
myForm.TextBox1.DataBindings.Add(new Binding("Enabled", viewModel, "IsFormEnabled"));
myForm.TextBox2.DataBindings.Add(new Binding("Enabled", viewModel, "IsFormEnabled"));
myForm.TextBox3.DataBindings.Add(new Binding("Enabled", viewModel, "IsFormEnabled"));
myForm.TextBox4.DataBindings.Add(new Binding("Enabled", viewModel, "IsFormEnabled"));
myForm.TextBox5.DataBindings.Add(new Binding("Enabled", viewModel, "IsFormEnabled"));
或者采用更优雅的方式:
List<TextBox> textBoxes = new List<TextBox>() { myForm.TextBox1, myForm.TextBox2, myForm.TextBox3, myForm.TextBox4, myForm.TextBox5 };
textBoxes.ForEach(t => t.DataBindings.Add(new Binding("Enabled", viewModel, "IsFormEnabled")));