将ComboxBox SelectedItem绑定到ViewModel的属性

时间:2018-07-27 14:30:08

标签: c# data-binding mvp bindingsource

我很难弄清楚如何将组合框选择的项目绑定到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

txtTelView是显示我将ViewModelcombobox上的其他控件绑定的示例。

但是如何将selecteItem CASE绑定到ViewModel属性?

0 个答案:

没有答案