Skip to content

Commit 9a6c63b

Browse files
Add Data binding tutorial using MVVM Toolkit (#5933)
* Update data binding overview and add how-to shell Update data binding overview, the TOC, and add the MVVM Toolkit data binding how-to topic with header and H1. * Add some intro info to the mvvm how-to * Minor tweaks * Update MVVM Toolkit tutorial * Update namespace declarations * Style and SEO edits * Fix broken link * Update customer intent * Refactor tutorial to multiple steps * Tweak TOC * Shorten code blocks for fakes * Add links to github code repo * Add more MVVM info and seo updates * Add WinUI class library background info * Fix broken links * Edits and update info about models
1 parent 90048fc commit 9a6c63b

7 files changed

Lines changed: 1524 additions & 7 deletions

File tree

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
---
22
ms.assetid: adfa70f3-a4d9-45d1-8957-c26a7703a276
3-
title: Data binding in Windows apps
4-
description: Data binding is a way for your app's UI to display data, and optionally to stay in sync with that data.
5-
ms.date: 12/12/2022
6-
ms.topic: article
3+
title: Data binding in Windows apps - A guide for developers
4+
description: Learn data binding in Windows apps to connect UI with data dynamically. Master x Bind and Binding markup extensions with examples and best practices for MVVM architecture.
5+
ms.date: 09/07/2025
6+
ms.topic: concept-article
77
keywords: windows 10, windows 11, windows app sdk, winui, windows ui
88
ms.localizationpriority: medium
9+
# customer intent: As a Windows developer, I want to learn about data binding in Windows apps so that I can create a separation of concerns between my data and UI.
910
---
1011

1112
# Data binding in Windows apps
1213

13-
Data binding is a way for your app's UI to display data, and optionally to stay in sync with that data. Data binding allows you to create a separation of concerns between your data and UI, and that results in a simpler conceptual model as well as better readability, testability, and maintainability of your app. In XAML markup, you can choose to use either the [{x:Bind} markup extension](/windows/apps/develop/platform/xaml/x-bind-markup-extension) or the [{Binding} markup extension](/windows/apps/develop/platform/xaml/binding-markup-extension). And you can even use a mixture of the two in the same app—even on the same UI element. `{x:Bind}` was new for UWP in Windows 10, is also available in Windows App SDK, and it has better performance.
14+
**Data binding** connects your app's user interface to its data, creating a dynamic relationship that keeps your UI current and responsive. When you implement data binding in Windows apps, you establish a clear separation between your app's data layer and presentation layer, which improves code organization and makes your app easier to maintain and test.
15+
16+
Windows apps support two primary data binding approaches: the [{x:Bind} markup extension](/windows/apps/develop/platform/xaml/x-bind-markup-extension) and the [{Binding} markup extension](/windows/apps/develop/platform/xaml/binding-markup-extension). You can use either approach individually or combine them within the same app. The `{x:Bind}` extension, available in Windows App SDK and UWP apps on Windows 10 and later, offers better performance and compile-time validation.
17+
18+
Whether you're displaying a single data item, binding to collections, or implementing complex architectural patterns like Model-View-ViewModel (MVVM), data binding provides the foundation for creating responsive, maintainable Windows applications.
1419

1520
| Topic | Description |
1621
|-------|-------------|
1722
| [Data binding overview](data-binding-overview.md) | 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). |
1823
| [Data binding in depth](data-binding-in-depth.md) | This topic describes data binding features in detail. |
1924
| [Bind hierarchical data and create a master/details view](bind-to-hierarchical-data-and-create-a-master-details-view.md) | You can make a multi-level master/details (also known as list-details) view of hierarchical data by binding items controls to [**CollectionViewSource**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.data.collectionviewsource) instances that are bound together in a chain. |
2025
| [Data binding and MVVM](data-binding-and-mvvm.md) | This topic describes the Model-View-ViewModel (MVVM) UI architectural design pattern. Data binding is at the core of MVVM, and enables loose coupling between UI and non-UI code. |
26+
| [How to data bind with the MVVM Toolkit in WinUI apps](../../tutorials/winui-mvvm-toolkit/intro.md) | This tutorial builds on the [Create a WinUI app](/windows/apps/tutorials/winui-notes/intro) tutorial and shows you how to implement data binding with the [MVVM Toolkit](/dotnet/communitytoolkit/mvvm/). It covers updating your view models to leverage the MVVM Toolkit and the differences between the MVVM Toolkit and traditional MVVM approaches. |
2127
| [Functions in x:Bind](function-bindings.md) | In Windows App SDK apps, `{x:Bind}` supports using a function as the leaf step of the binding path. In this topic, learn how properties are bound to functions to do conversions, date formatting, text formatting, text concatenations, etc. |
2228

23-
## See also
29+
## Related content
2430

25-
* [Data binding in UWP apps](/windows/uwp/data-binding/)
31+
- [Data binding in UWP apps](/windows/uwp/data-binding/)

hub/apps/toc.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ items:
196196
href: develop/data-binding/bind-to-hierarchical-data-and-create-a-master-details-view.md
197197
- name: Data binding and MVVM
198198
href: develop/data-binding/data-binding-and-mvvm.md
199+
- name: Data binding with the MVVM Toolkit
200+
items:
201+
- name: Introduction
202+
href: tutorials/winui-mvvm-toolkit/intro.md
203+
- name: 1 - Create class library project
204+
href: tutorials/winui-mvvm-toolkit/class-library.md
205+
- name: 2 - Implement MVVM
206+
href: tutorials/winui-mvvm-toolkit/mvvm-implementation.md
207+
- name: 3 - Add dependency injection
208+
href: tutorials/winui-mvvm-toolkit/dependency-injection.md
209+
- name: 4 - Add unit tests
210+
href: tutorials/winui-mvvm-toolkit/unit-testing.md
199211
- name: Files, folders, and libraries
200212
items:
201213
- name: Files, folders, and libraries
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: Data Binding with WinUI and MVVM Toolkit - Step 1 - Create a class library project
3+
description: Create a separate class library project to hold ViewModels and services for improved testability in your WinUI app.
4+
ms.date: 10/29/2025
5+
ms.topic: tutorial
6+
keywords: windows 11, windows app sdk, winui, windows ui, mvvm, mvvm toolkit, dotnet
7+
ms.localizationpriority: medium
8+
---
9+
10+
# Create a class library project
11+
12+
To enable unit testing of your ViewModels and services, create a separate class library project. You need this project because WinUI unit test projects can't directly reference WinUI app projects.
13+
14+
## Understanding the WinUI Class Library template
15+
16+
The **WinUI Class Library** project template creates a .NET managed class library (DLL) specifically designed for use with WinUI 3 desktop applications. This template is part of the Windows App SDK and provides important capabilities that a standard .NET class library doesn't include.
17+
18+
### Key differences from a .NET Class Library
19+
20+
The WinUI Class Library template differs from a standard .NET Class Library in several important ways:
21+
22+
- **Windows-specific targeting**: It targets a Windows-specific framework (like `net8.0-windows10.0.19041.0`) rather than the cross-platform .NET framework, giving access to Windows APIs.
23+
- **Windows App SDK integration**: It includes references to the `Microsoft.WindowsAppSDK` and `Microsoft.Windows.SDK.BuildTools` NuGet packages, providing access to WinUI 3 and Windows App SDK APIs.
24+
- **WinUI support enabled**: The project includes `<UseWinUI>true</UseWinUI>` in its configuration, enabling WinUI-specific build tasks and XAML compilation.
25+
- **Windows runtime identifiers**: It's configured for Windows-specific runtime identifiers (win-x86, win-x64, win-arm64).
26+
27+
### Why use a WinUI Class Library?
28+
29+
Use the **WinUI Class Library** template instead of a regular **.NET Class Library** when your library needs to:
30+
31+
- **Reference WinUI 3 types and controls**: The WinUI Class Library template allows you to use types from the `Microsoft.UI.Xaml` namespace and other Windows App SDK APIs in your library code.
32+
- **Include XAML resources**: If your library contains UserControls, custom controls, or other XAML resources, you need the WinUI Class Library template to properly compile and package these resources.
33+
- **Integrate with WinUI apps**: The template is configured to work seamlessly with WinUI 3 desktop apps, ensuring compatibility with the Windows App SDK runtime and deployment model.
34+
- **Support XAML markup compilation**: The template includes the necessary build tasks to compile XAML files into the library.
35+
36+
### When to use a regular .NET Class Library
37+
38+
Use a standard **.NET Class Library** project when your library:
39+
40+
- Contains only pure .NET code (ViewModels, models, services, utilities)
41+
- Doesn't reference any WinUI 3 or Windows App SDK types
42+
- Doesn't include any XAML files or UI-related code
43+
- Needs to be shared across different application types (not just WinUI apps)
44+
- Targets multiple platforms (for example, .NET MAUI or ASP.NET Core) or operating systems (for example, Linux or macOS)
45+
46+
For a tutorial on adding a .NET Class Library to your solution, see [Extend C# console app and debug in Visual Studio](/visualstudio/get-started/csharp/tutorial-console-part-2).
47+
48+
For this tutorial, use the **WinUI Class Library** template because it allows you to reference WinUI types if needed in the future, and it's specifically designed to integrate with WinUI 3 applications. While our ViewModels and services don't currently require WinUI types, using this template provides flexibility and ensures proper integration with the Windows App SDK environment.
49+
50+
## Create the WinUINotes.Bus project
51+
52+
Create a new WinUI Class Library project named `WinUINotes.Bus` to hold your ViewModels, models, and services in the same solution as your WinUI app project.
53+
54+
1. In Visual Studio, right-click the solution in **Solution Explorer**.
55+
1. Select **Add** > **New Project...**.
56+
1. Choose the **WinUI Class Library** template and select **Next**.
57+
58+
> [!NOTE]
59+
> Make sure you select **WinUI Class Library**, not just **Class Library**. The WinUI Class Library template includes references to the Windows App SDK and WinUI 3 framework.
60+
61+
1. Name the project `WinUINotes.Bus` and select **Create**.
62+
1. Delete the default `Class1.cs` file.
63+
64+
## Add project references
65+
66+
Project references enable your WinUI app project to use the ViewModels and services defined in the class library project:
67+
68+
1. Right-click the **WinUINotes** project and select **Add** > **Project Reference...**.
69+
1. Check the **WinUINotes.Bus** project and select **OK**.
70+
71+
The Bus project contains your ViewModels, models, and services, so you can test them independently of the UI layer.
72+
73+
> [!NOTE]
74+
> The term "Bus" indicates a project that acts as a communication layer or intermediary. It contains the presentation logic (ViewModels), business logic (models), and services that you can share and test independently of the UI.
75+
76+
> [!div class="nextstepaction"]
77+
> [Continue to step 2 - Implement MVVM with the MVVM Toolkit](mvvm-implementation.md)

0 commit comments

Comments
 (0)