Skip to content

Commit e977c07

Browse files
authored
Merge pull request #50043 from ShawnKupfer/WB1749
AB#1044063: Create a UI that uses data binding in .NET MAUI
2 parents baa354c + 6be8fa8 commit e977c07

15 files changed

+88
-95
lines changed

learn-pr/dot-net-maui/separate-ui-and-logic-with-data-binding/1-introduction.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Introduction
44
metadata:
55
title: Introduction
66
description: Move app logic to objects that are bound to the apps UI. Data bindings can make your code faster to read, easier to maintain, and more testable.
7-
ms.date: 02/15/2024
7+
ms.date: 04/16/2025
88
author: adegeo
99
ms.author: adegeo
1010
ms.topic: unit

learn-pr/dot-net-maui/separate-ui-and-logic-with-data-binding/2-compare-event-and-data-driven.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Compare an event-driven UI to a data-bound UI
44
metadata:
55
title: Compare an event-driven UI to a data-bound UI
66
description: In this unit, you'll compare an event-driven UI to a data-bound UI.
7-
ms.date: 02/15/2024
7+
ms.date: 04/16/2025
88
author: adegeo
99
ms.author: adegeo
1010
ms.topic: unit
@@ -18,15 +18,15 @@ quiz:
1818
choices:
1919
- isCorrect: false
2020
content: "You never have to write code related to your UI again."
21-
explanation: "While you could design a code-less UI, you don't have to, and it's not the most important feature of data binding. Data binding is beneficial because it provides a system where you have real-time updates to the UI when the underlying data changes."
21+
explanation: "While you could design a codeless UI, you don't have to, and it's not the most important feature of data binding. Data binding is beneficial because it provides a system where you have real-time updates to the UI when the underlying data changes."
2222
- isCorrect: true
2323
content: "The UI is automatically kept in sync with the data."
24-
explanation: "One benefit of adding data binding your UI, is that it keeps the UI elements in sync with the properties of the data object. It also helps separate app logic from app presentation."
24+
explanation: "One benefit of adding data binding your UI is that it keeps the UI elements in sync with the properties of the data object. It also helps separate app logic from app presentation."
2525
- content: "In an app that retrieves the latest weather, a label control that represents the temperature is bound to the data object. When the user presses the \"Get Weather\" button, triggering the `Clicked` event, what would the event handler code do to update the UI?"
2626
choices:
2727
- isCorrect: true
2828
content: "From the event handler, invoke the weather service and update the weather data object with the latest information."
29-
explanation: "Since the code-behind is only \"talking\" to the data object and not the label, it isn't tightly coupled to the label. The label automatically updates its text when the bound data object is changed."
29+
explanation: "Because the code-behind is only \"talking\" to the data object and not the label, it isn't tightly coupled to the label. The label automatically updates its text when the bound data object is changed."
3030
- isCorrect: false
3131
content: "From the event handler, do nothing. The temperature label is data bound so it should update automatically."
3232
explanation: "Even though the text of the label is bound to the data object, something still needs to retrieve the latest data and put it into the data object. Otherwise, if the data object doesn't update, neither does the UI."

learn-pr/dot-net-maui/separate-ui-and-logic-with-data-binding/3-use-data-bindings-xaml.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Use data bindings in XAML
44
metadata:
55
title: Use data bindings in XAML
66
description: In this unit, you'll learn about how to declare and use data bindings in XAML.
7-
ms.date: 02/15/2024
7+
ms.date: 04/16/2025
88
author: adegeo
99
ms.author: adegeo
1010
ms.topic: unit

learn-pr/dot-net-maui/separate-ui-and-logic-with-data-binding/4-exercise-replace-code-with-data-bindings.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Exercise - Replace code with .NET MAUI bindings
44
metadata:
55
title: Exercise - Replace code with .NET MAUI bindings
66
description: Learn how to convert a XAML UI that uses event handlers to display data, to a UI that uses data binding.
7-
ms.date: 02/15/2024
7+
ms.date: 04/16/2025
88
author: adegeo
99
ms.author: adegeo
1010
ms.topic: unit

learn-pr/dot-net-maui/separate-ui-and-logic-with-data-binding/5-databinding-mismatched-types.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Data binding with mismatched types
44
metadata:
55
title: Data binding with mismatched types
66
description: Learn how to data binding between two mismatched property types.
7-
ms.date: 02/15/2024
7+
ms.date: 04/16/2025
88
author: adegeo
99
ms.author: adegeo
1010
ms.topic: unit

learn-pr/dot-net-maui/separate-ui-and-logic-with-data-binding/6-exercise-create-converter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Exercise - Create a .NET MAUI converter
44
metadata:
55
title: Exercise - Create a .NET MAUI converter
66
description: Learn how to implement a converter that converts an enumeration to an image resource.
7-
ms.date: 02/15/2024
7+
ms.date: 04/16/2025
88
author: adegeo
99
ms.author: adegeo
1010
ms.topic: unit

learn-pr/dot-net-maui/separate-ui-and-logic-with-data-binding/7-summary.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Summary
44
metadata:
55
title: Summary
66
description: Summary
7-
ms.date: 02/15/2024
7+
ms.date: 04/16/2025
88
author: adegeo
99
ms.author: adegeo
1010
ms.topic: unit

learn-pr/dot-net-maui/separate-ui-and-logic-with-data-binding/includes/1-introduction.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
Data binding lets you declare relationships between your data and your user interface (UI) controls. They automatically stay in sync when either element changes. Data bindings can make your code faster to read, easier to maintain, and more testable.
22

3-
Data binding offers a way of coupling your UI directly to data instead of associating your UI with code in the UI's code-behind. When the UI is bound to data, the UI automatically updates when the data changes, staying in sync with the data. Oppositely If the user interacts with the UI, the data is kept in sync with the UI.
3+
Data binding offers a way of coupling your UI directly to data instead of associating your UI with code in the UI's code-behind. When the UI is bound to data, the UI automatically updates when the data changes, staying in sync with the data. Conversely, if the user interacts with the UI, the data stays in sync with the UI.
44

55
## Example scenario
66

7-
Imagine you're working on an app that presents users with weather information for a selected region or address. As the user inputs a location, the app loads the weather forecast data from an external weather service and updates the UI. The UI and the weather service data are tightly coupled via the XAML UI and the XAML code-behind file. The weather service is already abstracted to its own class, but your UI relies heavily on the code-behind to present that data to the UI.
7+
Imagine you're working on an app that presents users with weather information for a selected region or address. As the user inputs a location, the app loads the weather forecast data from an external weather service and updates the UI. The UI and the weather-service data are tightly coupled via the XAML UI and the XAML code-behind file. The weather service is already abstracted to its own class, but your UI relies heavily on the code-behind to present that data to the UI.
88

9-
Some time after your app is released, the weather service you use announces that they're going to be shutting down. Anticipating this, you start looking for a replacement service. Of the services you've evaluated, you notice that they all input and output data in different ways. Since your UI uses code-behind events for interaction with the service, your UI experience could be impacted by this change.
9+
Some time after your app is released, the weather service you use announces that they're shutting down. Anticipating this, you start looking for a replacement service. Of the services you've evaluated, you notice that they all input and output data in different ways. Because your UI uses code-behind events for interaction with the service, your UI experience could be impacted by this change.
1010

11-
It would be useful to change how the UI synchronizes the weather updates, from code-behind events to data binding. This way, if a service change happens again, you have minimized the impact to your code.
11+
It would be useful to change how the UI synchronizes the weather updates, from code-behind events to data binding. This way, if a service change happens again, you've minimized the impact to your code.
1212

13-
## What will you do
13+
## What you'll do
1414

1515
This module demonstrates the differences between a code-based UI and a data-bound UI and shows you how to use data binding to update the UI instead of code-behind. Using a sample weather app, you'll convert UI updates from code-behind to data binding.
1616

17-
## What do you learn
17+
## What you'll learn
1818

19-
By the end of this module, you're able to use data binding to present data in the UI, and transform data when the UI types and data types don't match.
19+
By the end of this module, you'll be able to use data binding to present data in the UI, and transform data when the UI types and data types don't match.
2020

2121
## Prerequisites
2222

learn-pr/dot-net-maui/separate-ui-and-logic-with-data-binding/includes/2-compare-event-and-data-bound.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
An event-driven user interface (UI) is designed around the events a control exposes. These events can be associated with event handler code that's invoked when the event is triggered. For example, let's say you have a button that when clicked performs a long-running operation. The event handler assigned to the `Clicked` event could start the operation and then set the button's `IsEnabled` property to `false`, preventing the button from being clicked again while the operation is running.
1+
An event-driven user interface (UI) is designed around the events a control exposes. These events can be associated with event-handler code that's invoked when the event is triggered. For example, let's say you have a button that, when clicked, performs a long-running operation. The event handler assigned to the `Clicked` event could start the operation and then set the button's `IsEnabled` property to `false`, preventing the button from being clicked again while the operation is running.
22

3-
A data-bound UI uses data binding to present and interact with data. Properties of controls are bound to the data object's properties, and those bindings can detect changes in the properties. Using the previous example, consider the button that performs a long-running operation. Instead of disabling the button in code-behind, the `IsEnabled` property is bound to the data object's `IsBusy` property. Whenever the data object becomes "busy" the button's enabled state is automatically changed to match.
3+
A data-bound UI uses data binding to present and interact with data. Properties of controls are bound to the data object's properties, and those bindings can detect changes in the properties. Using the previous example, consider the button that performs a long-running operation. Instead of disabling the button in code-behind, the `IsEnabled` property is bound to the data object's `IsBusy` property. Whenever the data object becomes "busy," the button's enabled state is automatically changed to match.
44

55
## Pros and cons of using events and code-behind
66

@@ -27,7 +27,7 @@ There are three named controls in this example:
2727
- `Button` control named **RefreshWeatherButton**.
2828
- `Label` control named **Humidity**.
2929

30-
The `RefreshWeatherButton` has an event handler declared for the `Clicked` event. When the button is clicked, the event handler queries a weather service for the latest weather forecast, using the data entered in the `PostalCode` entry control, and sets the `Humidity` label's text to the current humidity.
30+
The `RefreshWeatherButton` has an event handler declared for the `Clicked` event. When the button is clicked, the event handler queries a weather service for the latest weather forecast using the data entered in the `PostalCode` entry control, and sets the `Humidity` label's text to the current humidity.
3131

3232
```csharp
3333
private void RefreshWeatherButton_Clicked(object sender, EventArgs e)
@@ -40,11 +40,11 @@ private void RefreshWeatherButton_Clicked(object sender, EventArgs e)
4040

4141
In this one event handler, three controls are tightly coupled to each other and the data through the code-behind.
4242

43-
This design works great for small UIs, but as soon as the UI becomes complex, maintaining a tightly coupled code-behind can become troublesome. If you delete or change a control, you must clean up any code using those UI controls, which could include the event handler. If you decide to redesign the UI, you'll have lots of code to refactor too. And when the backing data structure changes, you have to dive into the code of each UI to stay in sync.
43+
This design works great for small UIs, but as soon as the UI becomes complex, maintaining a tightly coupled code-behind can become troublesome. If you delete or change a control, you must clean up any code using those UI controls, which could include the event handler. If you decide to redesign the UI, you'll have lots of code to refactor, too. And when the backing data structure changes, you have to dive into the code of each UI to stay in sync.
4444

4545
## Data binding helps
4646

47-
Data bindings can be implemented in XAML or code, but are much more common in XAML where they help to reduce the size of the code-behind file. By replacing procedural code in event handlers with declarative code or markup, the app is simplified and clarified. Because the bindings don't require code-behind, you can easily create, alter, or redesign the UI to fit how you want to present the data.
47+
You can implement data bindings in XAML or code, but they're much more common in XAML, where they help to reduce the code-behind file size. By replacing procedural code in event handlers with declarative code or markup, the app is simplified and clarified. Because the bindings don't require code-behind, you can easily create, alter, or redesign the UI to fit how you want to present the data.
4848

4949
Let's take the same example as in the previous section, but update it to use data binding:
5050

@@ -59,30 +59,24 @@ Let's take the same example as in the previous section, but update it to use dat
5959
</VerticalStackLayout>
6060
```
6161

62-
You can spot the properties that are data bound, they use the XAML extension syntax `{Binding ...}` for the value of the property. Don't worry about the specifics yet, that is covered later in this module.
62+
You can spot the properties that are data bound, they use the XAML extension syntax `{Binding ...}` for the value of the property. Don't worry about the specifics yet; we cover that later in this module.
6363

64-
The same three controls are declared in the XAML, but none of them are named, as a name isn't required:
64+
The same three controls are declared in the XAML, but none of them are named, because a name isn't required:
6565

66-
- `Entry` control:
66+
- **`Entry` control**: This control's `Text` property is bound to a property named `Location`.
6767

68-
This control's `Text` property is bound to a property named `Location`.
68+
- **`Button` control**: The button's `Command` property is bound to a property named `RefreshWeather`. `Command` is a property on the button that invokes code when the button is pressed. It's an alternative to the `Clicked` event that's used in data binding.
6969

70-
- `Button` control:
71-
72-
The button's `Command` property is bound to a property named `RefreshWeather`. `Command` is a property on the button that invokes code when the button is pressed. It's an alternative to the `Clicked` event that's used in data binding.
73-
74-
- `Label` control:
75-
76-
This `Text` property is bound to a property named `Humidity`.
70+
- **`Label` control**: This `Text` property is bound to a property named `Humidity`.
7771

7872
In this simple UI, all of the code-behind is eliminated. Removing all code-behind isn't the point of data binding, even though it's usually possible. Code-behind still has its place. How much data binding you implement is up to you.
7973

80-
Now the UI is loosely coupled to a data object. Why is it loosely coupled instead of tightly coupled? Because of the way bindings are evaluated. Each control has a `BindingContext` property. If the context isn't set, the parent control's context is used, and so on, until the root of the XAML is evaluated. When bindings are evaluated, the context's object instance is checked for the required properties, like the label control's `Text` binding to the context's `Humidity` property. If `Humidity` doesn't exist on the context, nothing happens.
74+
Now, the UI is loosely coupled to a data object. Why is it loosely coupled instead of tightly coupled? Because of the way bindings are evaluated. Each control has a `BindingContext` property. If the context isn't set, the parent control's context is used, and so on, until the root of the XAML is evaluated. When bindings are evaluated, the context's object instance is checked for the required properties, like the label control's `Text` binding to the context's `Humidity` property. If `Humidity` doesn't exist on the context, nothing happens.
8175

82-
Because the UI is loosely coupled, you can redesign the UI without the worry of breaking code. However, you can break functionality. For example, you can delete the button and the app still compiles and runs, but you don't have a way to refresh the weather. On the other hand, you could replace the `Entry` and `Button` controls with the single `SearchBar` control. This control lets you enter text and invoke a command.
76+
Because the UI is loosely coupled, you can redesign the UI worrying about breaking code. However, you can break functionality. For example, you can delete the button and the app still compiles and runs, but you don't have a way to refresh the weather. On the other hand, you could replace the `Entry` and `Button` controls with the single `SearchBar` control. This control lets you enter text and invoke a command.
8377

8478
```xaml
8579
<SearchBar Text="{Binding Location, Mode=OneWayToSource}" SearchCommand="{Binding RefreshWeather}" />
8680
```
8781

88-
As you can see, using data binding in your UI design can help you evolve and change your UI without much work. It keeps the UI synchronized with the data automatically and the app logic is separated from the UI.
82+
As you can see, using data binding in your UI design can help you evolve and change your UI without much work. It keeps the UI synchronized with the data automatically, and the app logic is separated from the UI.

0 commit comments

Comments
 (0)