-
Notifications
You must be signed in to change notification settings - Fork 49
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
eriklimakc
wants to merge
4
commits into
main
Choose a base branch
from
dev/erli/csm-datatemplate-binding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−0
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--- | ||
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 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` 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>(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` into a helper method and take advantage of the `RelativeSource` method to provide the `CommandParameter`. | ||
|
||
```csharp | ||
... | ||
|
||
.Children( | ||
new TextBlock() | ||
.Text(() => item.Text), | ||
CreateRemoveButton() | ||
) | ||
... | ||
|
||
private Button CreateRemoveButton() => 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) | ||
); | ||
``` | ||
|
||
Given that `CommandParameter` is the `DataContext` of the `ListView.ItemTemplate`, we can simplify it by using the XAML equivalent of `{Binding .}`. In C# Markup this is `(x => x.Binding())`: | ||
|
||
```csharp | ||
private Button CreateRemoveButton() => new Button() | ||
.Content("Delete") | ||
.Command(x => x.Source(this) | ||
.DataContext<MainViewModel>() | ||
.Binding(vm => vm.RemoveItemCommand) | ||
) | ||
.CommandParameter(x => x.Binding()); | ||
``` | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have an inconsistency issue here:
RelativeSource as
TemplatedParent
works on Windows but not on UnoRelativeSource as
Self
works on Uno but not on WindowsShould be fixed by unoplatform/uno#12732
@dansiegel @Xiaoy312 @agneszitte
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uno issue: unoplatform/uno#16940
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eriklimakc @dansiegel I'm not sure what this example is supposed to demonstrate? Is this supposed to be the equivalent of CommandParameter="{Binding }" in XAML?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I believe it is how you would accomplish
CommandParameter="{Binding }"
when you are in an extracted method that doesn't have a reference to the datacontextThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's to achieve what @kazo0 mentioned and in the next example we also mention the possiblity of doing
.CommandParameter(x => x.Binding())
to have the same result withoutRelativeSource
.