Binding to a collection
The previous recipe showed how to bind various properties to a single object, using single data controls, such as TextBlock
, TextBox
, or Button
. Some controls, however, are capable of displaying multiple object data, such as a ListBox
. These controls can bind to a collection of data objects, and with the help of data templates and value converters (to be covered in later recipes), become a powerful visualization tool. Let's examine the basic ideas of binding to collection of objects.
Getting ready
Make sure Visual Studio is up and running.
How to do it...
We'll create a collection of Person
object and bind them to a ListBox
, showing the details we need to take care of.
Create a new WPF Application named
CH06.BindingToCollection
.Open
MainWindow.xaml
. Add aListBox
to the existingGrid
and give it a name:<ListBox x:Name="_list"> </ListBox>
Create a new
Person
class, with two properties,Name
andAge
, similar to the previous recipe (no need forINotifyPropertyChanged...