In Part 4 of this mini-series on data binding we looked at Data Conversion. Today we look at binding a UI Element to a list of objects.
The trick in binding to a list, is to teach the control how to display the bound data. It needs to know how to display each object in the list – what properties of the object should be displayed and how should they appear?
You do this most often with a DataTemplate – a template that will be reproduced for each member of the collection. In the next example we create an Employee class. Each Employee has two properties, Name and Title. We also give the class a static method that returns a list of Employees – simulating retrieving data from a web service or other data source
Notice that the collection type we use for Employee is an ObservableCollection. This type implements INotifyPropertyChanged and INotifyCollectionChanged, and so will inform the UI if anything in the collection is changed.
Note that to ensure notification if an individual element in the collection is changed (e.g. an employee’s name changes) you must also notify INotifyPropertyChanged on the element type itself.
All we need now is to bind this to a control that handles collections of data, such as a ComboBox. The ComboBox has no way of knowing, however, how to display an Employee. Left to its own devices, it will just display whatever ToString resolves to. We could override ToString, but it is much more efficient and flexible to teach the ComboBox how to display the Employee exactly as we want, and we do that with a DataTemplate,
The ItemTemplate says “this is how you display each item” and the DataTemplate lays out how the data is displayed. In this case, we are displaying the data by laying out four TextBlocks horizontally so that each Employee appears on a single line in the ComboBox. Notice that we bind to the ItemsSource property with the keyword binding but we don’t specify what we’re binding to. That is done by setting the DataContext in the code-behind,
Source code is available here
Jesse Liberty has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and has been a Distinguished Software Engineer for AT&T and a VP for Information Services for Citibank and a Software Architect for PBS. You can read more on his personal blog or follow him on twitter
Subscribe to be the first to get our expert-written articles and tutorials for developers!