+4  Q: 

WPF Databinding

Can anyone point me to a good resource (or throw me a clue) to show me how to do databinding to controls (combobox, listbox, etc.) in WPF? I'm at a bit of a loss when all my WinForms niceities are taken away from me, and I'm not all that bright to start with...

Many Thanks, my fellow Stackies.

+5  A: 

The best resource I've found for WPF data binding is Bea Costa's blog. Start from the first post and read forward. It's awesome.

Matt Hamilton
+3  A: 

in code behind -- set the DataContext of your list box equal to the collection you're binding to.

private void OnInit(object sender, EventArgs e)
  //myDataSet is some IEnumerable 

  // myListBox is a ListBox control.
  // Set the DataContext of the ListBox to myDataSet
  myListBox.DataContext = myDataSet;

In XAML, Listbox can declare which properties it binds to using the "Binding" syntax.

<ListBox Name="myListBox" Height="200"
  ItemsSource="{Binding Path=BookTable}"
  ItemTemplate  ="{StaticResource BookItemTemplate}"/>
Leon Bambrick
+3  A: 

I find the tutorial videos at Windows Client .Net equally awesome. Dot Net Rocks TV has also covered it some time ago.

Mark Glorie

Wow, three great answers in an hour. You guys rock!

Viva La StackOverflow!

+3  A: 

And some more links, just in case the above didn't suffice:

Windows Presentation Foundation - Data Binding How-to Topics
- Approx 30 'How To' articles from MSDN.
"The topics in this section describe how to use data binding to bind elements to data from a variety of data sources in the form of common language runtime (CLR) objects and XML. "

Moving Toward WPF Data Binding One Step at a Time
- By WPF guru Josh Smith
"This article explains the absolute basics of WPF data binding. It shows four different ways how to perform the same simple task. Each iteration moves closer to the most compact, XAML-only implementation possible. This article is for people with no experience in WPF data binding."

David HAust
+2  A: 

Here's another good resource from MSDN: Data Binding Overview.

+1  A: 

There are three things you need to do:

  1. Bind the ItemsSource of the ComboBox to the list of options.
  2. Bind the SelectedItem to the property that holds the selection.
  3. Set the ComboBox.ItemTemplate to a DataTemplate for a ComboBoxItem.

So, for example, if your data context object is a person having email addresses, and you want to choose their primary, you might have classes with these signatures:

public class EmailAddress
    public string AddressAsString { get; set; }

public class Person
    public IEnumerable<EmailAddress> EmailAddresses { get; }
    public EmailAddress MainEmailAddress { get; set; }

Then you could create the combo box like this:

<ComboBox ItemsSource="{Binding EmailAddresses}" SelectedItem="{Binding MainEmailAddress}">
            <ComboBoxItem Content="{Binding AddressAsString}"/>

Now you need to implement INotifyPropertyChanged in both Person and EmailAddress. For the EmailAddresses collection, you could back it with an ObjservableCollection.

Or as an alternative you can use Update Controls .NET. This is an open source project that replaces data binding and does not require INotifyPropertyChanged. You can use whatever collection makes sense to back the EmailAddresses property. The XAML works the same as above, except that you import the UpdateControls.XAML namespace and replace {Binding ...} with {u:Update ...}.

Michael L Perry