我很难弄清楚如何将组合框选择的项目绑定到Data Source
上的属性,其中组合框的BindingSource
来自private ViewModel _viewModel;
public void BindControls(ViewModel viewModel)
{
_viewModel = viewModel;
BindingSourceComboBox();
SetupBindings();
}
private void BindingSourceComboBox()
{
bindingSource1.Add(new Person() {FirstName = "Jack", Surname = "Smith"});
bindingSource1.Add(new Person() { FirstName = "Joe", Surname = "Black" });
bindingSource1.Add(new Person() { FirstName = "James", Surname = "Brown" });
bindingSource1.Add(new Person() { FirstName = "Fred", Surname = "Mort" });
// Bind the combobox
cmb.DataSource = bindingSource1;
cmb.ValueMember = "FirstName";
}
private void SetupBindings()
{
txtName.DataBindings.Add("Text", bindingSource1, "FirstName");
txtSurname.DataBindings.Add("Text", bindingSource1, "Surname");
txtAdd.DataBindings.Add(nameof(TextBox.Text), _viewModel, nameof(_viewModel.Address), true, OnValidation, string.Empty, "C");
txtTel.DataBindings.Add(nameof(TextBox.Text), _viewModel, nameof(_viewModel.Tel), true, OnValidation, string.Empty, "C");
}
控件,如下所示:
查看
txtAdd
txtTel
和View
是显示我将ViewModel
到combobox
上的其他控件绑定的示例。
但是如何将selecteItem
CASE
绑定到ViewModel属性?