From 6d8d1112c97f29dca3565a8519baa56c06c833a4 Mon Sep 17 00:00:00 2001 From: eriklimakc Date: Wed, 29 May 2024 10:19:40 +0100 Subject: [PATCH] chore: Resolving csm binding docs comments --- doc/Learn/Markup/Binding-To-DataTemplates.md | 97 ++++++++++---------- 1 file changed, 46 insertions(+), 51 deletions(-) diff --git a/doc/Learn/Markup/Binding-To-DataTemplates.md b/doc/Learn/Markup/Binding-To-DataTemplates.md index 04f8337357..6d16a00e5b 100644 --- a/doc/Learn/Markup/Binding-To-DataTemplates.md +++ b/doc/Learn/Markup/Binding-To-DataTemplates.md @@ -4,38 +4,34 @@ uid: Uno.Extensions.Markup.BindToDataTemplate # Binding to DataTemplates -When working with `DataTemplate`s, accessing data or commands from the parent context can be challenging. `DataTemplate`s operate within their own scope, making it difficult to bind properties or trigger actions in the parent `DataContext`. This separation can lead to confusion, especially when dealing with nested templates and multiple levels of data context. +When working with `DataTemplate`s, accessing data or commands from the parent context can be challenging. `DataTemplate`s operate within their own scope, making it difficult to bind properties or trigger actions from the parent `DataContext`. This separation can lead to confusion, especially when dealing with nested templates and multiple layers of data context. -In the following example, the parent `DataContext` is the `ViewModel`. The `ViewModel` contains an `ICommand RemoveItemCommand`. The code demonstrates how to access that `ICommand` inside the `ItemTemplate` of the `ListView`. +In the following example, the parent `DataContext` is the `ViewModel` that contains a `RemoveItemCommand`. The code demonstrates how to access that `ICommand` within the `ListView`'s `ItemTemplate`. ```csharp -this.DataContext(new ViewModel(), (page, vm) - => page - .Content( - new ListView() - .ItemsSource(() => vm.Items) - .ItemTemplate(item => - new StackPanel() - .Children( - new TextBlock() - .Text(() => item.Text), - new Button() - .Content("Delete") - .CommandParameter(() => item) - - // Since we have access to the `page` and `vm` alias from the DataContext method - // We can take advantage of them and use them on our binding expression - .Command(x => x.Source(page) - .DataContext() - .Binding(() => vm.RemoveItemCommand) - ) - ) - ) - ) -) +this.DataContext(new ViewModel(), (page, vm) => page.Content( + new ListView() + .ItemsSource(() => vm.Items) + .ItemTemplate(item => + new StackPanel() + .Children( + new TextBlock().Text(() => item.Text), + new Button() + .Content("Delete") + .CommandParameter(() => item) + // Since we have access to the `page` and `vm` alias from the DataContext method + // We can take advantage of them and use them on our binding expression + .Command(x => x + .Source(page) + .DataContext() + .Binding(() => vm.RemoveItemCommand) + ) + ) + ) +)) ``` -Alternatively, we could extract the `Button` to a helper method and take advantage of the `RelativeSource` method to provide the `CommandParameter`. +Alternatively, we could extract the `Button` into a helper method and take advantage of the `RelativeSource` method to provide the `CommandParameter`. ```csharp ... @@ -43,37 +39,36 @@ Alternatively, we could extract the `Button` to a helper method and take advanta .Children( new TextBlock() .Text(() => item.Text), - CreateButton() + CreateRemoveButton() ) ... -private Button CreateButton() - => new Button() - .Content("Delete") - .CommandParameter(x => x.RelativeSource