Skip to content

Commit 8361dd9

Browse files
Merge pull request #5741 from MicrosoftDocs/main
Auto Publish – main to live - 2025-08-27 05:00 UTC
2 parents 4e77476 + b467021 commit 8361dd9

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

hub/apps/develop/data-binding/data-binding-overview.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
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
66
ms.topic: concept-article
77
keywords: windows 10, windows 11, windows app sdk, winui, windows ui
88
ms.localizationpriority: medium
@@ -13,17 +13,17 @@ dev_langs:
1313

1414
# Windows data binding overview
1515

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).
1717

1818
## Prerequisites
1919

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/).
2121

2222
## Create the project
2323

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".
2525

26-
## Binding to a single item
26+
## Bind to a single item
2727

2828
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.
2929

@@ -54,7 +54,7 @@ namespace Quickstart
5454
}
5555
public class RecordingViewModel
5656
{
57-
private Recording defaultRecording = new Recording();
57+
private Recording defaultRecording = new();
5858
public Recording DefaultRecording { get { return defaultRecording; } }
5959
}
6060
}
@@ -70,9 +70,8 @@ namespace Quickstart
7070
public MainWindow()
7171
{
7272
this.InitializeComponent();
73-
ViewModel = new RecordingViewModel();
7473
}
75-
public RecordingViewModel ViewModel{ get; set; }
74+
public RecordingViewModel ViewModel{ get; } = new RecordingViewModel();
7675
}
7776
}
7877
```
@@ -83,8 +82,8 @@ The last piece is to bind a `TextBlock` to the `ViewModel.DefaultRecording.OneLi
8382
<Window x:Class="Quickstart.MainWindow" ... >
8483
<Grid>
8584
<TextBlock Text="{x:Bind ViewModel.DefaultRecording.OneLineSummary}"
86-
HorizontalAlignment="Center"
87-
VerticalAlignment="Center"/>
85+
HorizontalAlignment="Center"
86+
VerticalAlignment="Center"/>
8887
</Grid>
8988
</Window>
9089
```
@@ -93,18 +92,21 @@ Here's the result.
9392

9493
![Binding a textblock](images/xaml-databinding0.png)
9594

96-
## Binding to a collection of items
95+
## Bind to a collection of items
9796

98-
A common scenario is to bind to a collection of business objects. In C#, the generic [ObservableCollection&lt;T&gt;](/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&lt;T&gt;](/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&lt;T&gt;](/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.
99101
100102
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.
101103

102104
``` csharp
103105
public class RecordingViewModel
104106
{
105107
...
106-
private ObservableCollection<Recording> recordings = new ObservableCollection<Recording>();
107-
public ObservableCollection<Recording> Recordings{ get{ return recordings; } }
108+
private List<Recording> recordings = new();
109+
public List<Recording> Recordings{ get{ return recordings; } }
108110
public RecordingViewModel()
109111
{
110112
recordings.Add(new Recording(){ ArtistName = "Johann Sebastian Bach",
@@ -123,7 +125,8 @@ And then bind a [ListView](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.
123125
<Window x:Class="Quickstart.MainWindow" ... >
124126
<Grid>
125127
<ListView ItemsSource="{x:Bind ViewModel.Recordings}"
126-
HorizontalAlignment="Center" VerticalAlignment="Center"/>
128+
HorizontalAlignment="Center"
129+
VerticalAlignment="Center"/>
127130
</Grid>
128131
</Window>
129132
```
@@ -168,7 +171,7 @@ HorizontalAlignment="Center" VerticalAlignment="Center">
168171

169172
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).
170173

171-
## Adding a details view
174+
## Add a details view
172175

173176
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.
174177

@@ -231,7 +234,7 @@ And here's the identical result in each case.
231234

232235
![Binding a list view 4](images/xaml-databinding4.png)
233236

234-
## Formatting or converting data values for display
237+
## Format or convert data values for display
235238

236239
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.
237240

0 commit comments

Comments
 (0)