--- title: "How to: Bind to a Collection and Display Information Based on Selection" description: Follow this example to find out how to bind to a collection and display information based on selection in the Windows Presentation Foundation (WPF). ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "data collections [WPF], selecting data for views" - "data binding [WPF], creating views of data collections" - "data binding [WPF], selecting data for views" - "data binding [WPF], binding to collections" ms.assetid: 952a7d76-dd29-49e5-86f5-32c4530e70eb --- # How to: Bind to a Collection and Display Information Based on Selection In a simple master-detail scenario, you have a data-bound such as a . Based on user selection, you display more information about the selected item. This example shows how to implement this scenario. ## Example In this example, `People` is an of `Person` classes. This `Person` class contains three properties: `FirstName`, `LastName`, and `HomeTown`, all of type `string`. [!code-xaml[CollectionBinding#Source](~/samples/snippets/csharp/VS_Snippets_Wpf/CollectionBinding/CSharp/Window1.xaml#source)] [!code-xaml[CollectionBinding#UI](~/samples/snippets/csharp/VS_Snippets_Wpf/CollectionBinding/CSharp/Window1.xaml#ui)] The uses the following that defines how the information of a `Person` is presented: [!code-xaml[CollectionBinding#DetailTemplate](~/samples/snippets/csharp/VS_Snippets_Wpf/CollectionBinding/CSharp/Window1.xaml#detailtemplate)] The following is a screenshot of what the example produces. The shows the other properties of the person selected. ![Binding to a collection](./media/databinding-collectionbindingsample.png "DataBinding_CollectionBindingSample") The two things to notice in this example are: 1. The and the bind to the same source. The properties of both bindings are not specified because both controls are binding to the entire collection object. 2. You must set the property to `true` for this to work. Setting this property ensures that the selected item is always set as the . Alternatively, if the gets it data from a , it synchronizes selection and currency automatically. Note that the `Person` class overrides the `ToString` method the following way. By default, the calls `ToString` and displays a string representation of each object in the bound collection. That is why each `Person` appears as a first name in the . [!code-csharp[CollectionBinding#ToString](~/samples/snippets/csharp/VS_Snippets_Wpf/CollectionBinding/CSharp/Data.cs#tostring)] [!code-vb[CollectionBinding#ToString](~/samples/snippets/visualbasic/VS_Snippets_Wpf/CollectionBinding/VisualBasic/Person.vb#tostring)] ## See also - [Use the Master-Detail Pattern with Hierarchical Data](how-to-use-the-master-detail-pattern-with-hierarchical-data.md) - [Use the Master-Detail Pattern with Hierarchical XML Data](how-to-use-the-master-detail-pattern-with-hierarchical-xml-data.md) - [Data Binding Overview](/dotnet/desktop-wpf/data/data-binding-overview) - [Data Templating Overview](data-templating-overview.md) - [How-to Topics](data-binding-how-to-topics.md)