Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add Binding to DataTemplates docs #2292

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions doc/Learn/Markup/Binding-To-DataTemplates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
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.
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved

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`.
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved

```csharp
this.DataContext(new ViewModel(), (page, vm)
=> page
.Content(
new ListView()
.ItemsSource(() => vm.Items)
.ItemTemplate<Item>(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)
)
)
)
)
)
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved
```

Alternatively, we could extract the `Button` to a helper method and take advantage of the `RelativeSource` method to provide the `CommandParameter`.
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved

```csharp
...

.Children(
new TextBlock()
.Text(() => item.Text),
CreateButton()
)
...

private Button CreateButton()
=> new Button()
.Content("Delete")
.CommandParameter(x => x.RelativeSource<Button>(RelativeSourceMode.TemplatedParent)
.Binding(btn => btn.DataContext)
)
.Command(x => x
// Since we don't have access to the `page` alias here (as we have in the previous example)
// We need to set `this` as the source
.Source(this)
// Here we specify the DataContext type so that we can access the ViewModel alias in the Binding method
.DataContext<MainViewModel>()
.Binding(vm => vm.RemoveItemCommand)
);
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved
```

Since the `CommandParameter` we're providing is the `DataContext` of the `ItemTemplate` from the `ListView`, we can simplify it by using the XAML equivalent of `{Binding .}`. In C# Markup this is `(x => x.Binding())`:
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved

```csharp
private Button CreateButton()
=> new Button()
.Content("Delete")
.CommandParameter(x => x.Binding())
.Command(x => x.Source(this)
.DataContext<MainViewModel>()
.Binding(vm => vm.RemoveItemCommand)
);
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved
```

For more information about `Source` and `RelativeSource` usages in C# Markup, refer to the [Source and Relative Source](xref:Uno.Extensions.Markup.SourceUsage) documentation.

## Next Steps

- [Binding, Static & Theme Resources](xref:Uno.Extensions.Markup.DependencyPropertyBuilder)
- [Binding 101](xref:Uno.Extensions.Markup.Binding101)
- [Attached Properties](xref:Uno.Extensions.Markup.AttachedProperties)
- [Styles](xref:Uno.Extensions.Markup.Styles)
- [Templates](xref:Uno.Extensions.Markup.Templates)
- [VisualStateManagers](xref:Uno.Extensions.Markup.VisualStateManager)
- [Generating C# Extensions for your libraries](xref:Uno.Extensions.Markup.GeneratingExtensions)
2 changes: 2 additions & 0 deletions doc/Learn/Markup/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
href: xref:Uno.Extensions.Markup.Binding101
- name: Source and Relative Source
href: xref:Uno.Extensions.Markup.SourceUsage
- name: Binding to DataTemplates
href: xref:Uno.Extensions.Markup.BindToDataTemplate
- name: Converters
href: xref:Uno.Extensions.Markup.Converters
- name: Using Static & Theme Resources
Expand Down
Loading