Can databinding in WPF be done simply with a dataview like in winforms?
Short and simple question. In Winforms you can bind a dataview to a combobox or some other control by simply:
combobox.DataSource = dataview combobox.DisplayMember = "Something"
In WPF I've generally done databinding using an ObservableCollection and edits to the xaml. Is there a way to quickly do it, like the above?
Edit: This seems to be the simplest/quickest thing I can come up with, anything inherently wrong with it?
combobox.ItemSource = dataview combobox.DisplayMemberPath = "Something"
You can set binding programmatically, although, per my understanding of the MVVM pattern, the best practice is to set binding in the View (xaml) not the ViewModel or View code-behind.
How to set binding programmatically:
Binding myBinding = new Binding("Name"); myBinding.Source = dataview // data source from your example combobox.DisplayMemberPath = "Something" combobox.SetBinding(ComboBox.ItemsSourceProperty, myBinding);
With this, when your dataview is updated, the updates will be shown in your ComboBox.
You can do this:
List<Person> someListOFPersons = new List<Person>(); comboBox.DataContext = someListOfPersons; comboBox.DisplayMemberPath = "FirstName";
You will not see changes in the collection. So if a person is added to or removed from the list the combobox will not see it.