Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 2.04 KB

how-to-bind-to-the-results-of-a-linq-query.md

File metadata and controls

33 lines (24 loc) · 2.04 KB
title description ms.date helpviewer_keywords ms.assetid
How to: Bind to the Results of a LINQ Query
Learn how to run a LINQ query and then bind to the results in Windows Presentation Foundation (WPF).
03/30/2017
running a LINQ query [WPF], bind to results
binding to LINQ query results [WPF]
ff2844d9-17ed-4ea6-aab1-5111af0bc684

How to: Bind to the Results of a LINQ Query

This example demonstrates how to run a LINQ query and then bind to the results.

Example

The following example creates two list boxes. The first list box contains three list items.

[!code-xamlLinqExample#UI]

Selecting an item from the first list box invokes the following event handler. In this example, Tasks is a collection of Task objects. The Task class has a property named Priority. This event handler runs a LINQ query that returns the collection of Task objects that have the selected priority value, and then sets that as the xref:System.Windows.FrameworkElement.DataContext%2A:

[!code-csharpLinqExample#Using] [!code-csharpLinqExample#Tasks] [!code-csharpLinqExample#Handler]

The second list box binds to that collection because its xref:System.Windows.Controls.ItemsControl.ItemsSource%2A value is set to {Binding}. As a result, it displays the returned collection (based on the myTaskTemplate xref:System.Windows.DataTemplate).

See also