You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: hub/apps/develop/data-binding/data-binding-overview.md
+22-19Lines changed: 22 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
---
2
2
ms.assetid: 02a08657-285d-4804-a006-168c22aa4904
3
-
title: Windows data binding overview for developers
4
-
description: This topic shows you how to bind a control (or other UI element) to a single item or bind an item's control to a collection of items in a WinUI app with Windows App SDK.
5
-
ms.date: 01/10/2025
3
+
title: Windows Data Binding Overview for Developers
4
+
description: Learn how to bind controls to data in WinUI apps with Windows App SDK. Bind controls to single items or collections, implement details views, and convert data for display.
5
+
ms.date: 08/26/2025
6
6
ms.topic: concept-article
7
7
keywords: windows 10, windows 11, windows app sdk, winui, windows ui
8
8
ms.localizationpriority: medium
@@ -13,17 +13,17 @@ dev_langs:
13
13
14
14
# Windows data binding overview
15
15
16
-
This topic shows you how to bind a control (or other UI element) to a single item or bind an items control to a collection of items in a Windows App SDK app. In addition, we show how to control the rendering of items, implement a details view based on a selection, and convert data for display. For more detailed info, see [Data binding in depth](data-binding-in-depth.md).
16
+
Data binding in WinUI apps with Windows App SDK allows you to connect controls to data sources efficiently. This article shows you how to bind a control to a single item or bind an items control to a collection of items. You'll also learn how to control item rendering, implement details views based on selections, and convert data for display. For more detailed info, see [Data binding in depth](data-binding-in-depth.md).
17
17
18
18
## Prerequisites
19
19
20
-
This topic assumes that you know how to create a basic Windows App SDK app. For instructions on creating your first Windows App SDK app, see [Create your first WinUI 3 (Windows App SDK) project](/windows/apps/winui/winui3/create-your-first-winui3-app).
20
+
This topic assumes that you know how to create a basic WinUI app with Windows App SDK. For instructions on creating your first WinUI app, see [Create a WinUI app](/windows/apps/tutorials/winui-notes/).
21
21
22
22
## Create the project
23
23
24
-
Create a new **Blank App, Packaged (WinUI 3 in Desktop)** C# project. Name it "Quickstart".
24
+
Create a new **WinUI Blank App, Packaged** C# project. Name it "Quickstart".
25
25
26
-
## Binding to a single item
26
+
## Bind to a single item
27
27
28
28
Every binding consists of a binding target and a binding source. Typically, the target is a property of a control or other UI element, and the source is a property of a class instance (a data model, or a view model). This example shows how to bind a control to a single item. The target is the `Text` property of a `TextBlock`. The source is an instance of a simple class named `Recording` that represents an audio recording. Let's look at the class first.
29
29
@@ -54,7 +54,7 @@ namespace Quickstart
54
54
}
55
55
publicclassRecordingViewModel
56
56
{
57
-
privateRecordingdefaultRecording=newRecording();
57
+
privateRecordingdefaultRecording=new();
58
58
publicRecordingDefaultRecording { get { returndefaultRecording; } }

95
94
96
-
## Binding to a collection of items
95
+
## Bind to a collection of items
97
96
98
-
A common scenario is to bind to a collection of business objects. In C#, the generic [ObservableCollection<T>](/dotnet/api/system.collections.objectmodel.observablecollection-1) class is a good collection choice for data binding, because it implements the [INotifyPropertyChanged](/dotnet/api/system.componentmodel.inotifypropertychanged) and [INotifyCollectionChanged](/dotnet/api/system.collections.specialized.inotifycollectionchanged) interfaces. These interfaces provide change notification to bindings when items are added or removed or a property of the list itself changes. If you want your bound controls to update with changes to properties of objects in the collection, the business object should also implement `INotifyPropertyChanged`. For more info, see [Data binding in depth](data-binding-in-depth.md).
97
+
A common scenario is to bind to a collection of business objects. In C#, the generic [ObservableCollection<T>](/dotnet/api/system.collections.objectmodel.observablecollection-1) class is typically recommended for data binding, because it implements the [INotifyCollectionChanged](/dotnet/api/system.collections.specialized.inotifycollectionchanged) interface, which provides change notification to bindings when items are added or removed. However, due to a known WinUI Release mode bug with .NET 8 and later, you may need to use a [List<T>](/dotnet/api/system.collections.generic.list-1) in some scenarios, especially if your collection is static and does not change after initialization. If your UI needs to update when the collection changes at runtime, use `ObservableCollection<T>`. If you only need to display a fixed set of items, `List<T>` is sufficient. Additionally, if you want your bound controls to update with changes to properties of objects in the collection, those objects should implement [INotifyPropertyChanged](/dotnet/api/system.componentmodel.inotifypropertychanged). For more info, see [Data binding in depth](data-binding-in-depth.md).
98
+
99
+
> [!NOTE]
100
+
> By using `List<T>`, you may not receive change notifications for collection changes. If you need to respond to changes, consider using `ObservableCollection<T>`. In our example, we don't need to respond to collection changes, so `List<T>` is sufficient.
99
101
100
102
This next example binds a [ListView](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.listview) to a collection of `Recording` objects. Let's start by adding the collection to our view model. Just add these new members to the `RecordingViewModel` class.
For more information about XAML syntax, see [Create a UI with XAML](/visualstudio/xaml-tools/creating-a-ui-by-using-xaml-designer-in-visual-studio). For more information about control layout, see [Define layouts with XAML](/windows/apps/design/layout/layouts-with-xaml).
170
173
171
-
## Adding a details view
174
+
## Add a details view
172
175
173
176
You can choose to display all the details of `Recording` objects in [ListView](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.listview) items. But that takes up a lot of space. Instead, you can show just enough data in the item to identify it and then, when the user makes a selection, you can display all the details of the selected item in a separate piece of UI known as the details view. This arrangement is also known as a master/details view, or a list/details view.
174
177
@@ -231,7 +234,7 @@ And here's the identical result in each case.
231
234
232
235

233
236
234
-
## Formatting or converting data values for display
237
+
## Format or convert data values for display
235
238
236
239
There is an issue with the rendering above. The `ReleaseDateTime` property is not just a date, it's a [DateTime](/uwp/api/windows.foundation.datetime). So, it's being displayed with more precision than we need. One solution is to add a string property to the `Recording` class that returns the equivalent of `ReleaseDateTime.ToString("d")`. Naming that property `ReleaseDate` would indicate that it returns a date, and not a date-and-time. Naming it `ReleaseDateAsString` would further indicate that it returns a string.
0 commit comments