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"

Answers


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.


Need Your Help

Implicit argument passing of super from method defined by define_method() is not supported

ruby-on-rails ruby ruby-1.9 formbuilder

In "Agile Web Development with Rails" (third edition) page 537 - 541 it has "Custom Form Builders" code as follows:

Kendo UI Ajax Request

ajax razor grid kendo-ui actionlink

I'm using Kendo UI Grid with an action link into a client-template column. This action link call an data edit view. See example: