x:Bind outside data templates is extremely inconsistent
See original GitHub issueDescribe the bug
Apparently it is possible to use x:Bind
to bind to a field in code behind outside a data template. Basically, escaping from the data template in use. Not entirely sure whether this is by design or just a lucky quirk of the XAML compiler and codegen (especially since this is not mentioned anywhere in the docs), but it works.
As a code example:
<UserControl
x:Class="MyNamespace.Views.MyUserControl"
xmlns:viewModels="using:MyNamespace.ViewModels"
models="using:MyNamespace.Models">
<UserControl.DataContext>
<viewModels:MyViewModel x:Name="ViewModel"/>
</UserControl.DataContext>
<UserControl.Resources>
<DataTemplate
x:Name="SomeDataTemplate"
x:DataTye="models:SomeModelTyppe">
<!--This works-->
<TextBlock Text="{x:Bind ViewModel.SomeText}"/>
</DataTemplate>
</UserControl.Resources>
<ListView
ItemsSource="{x:Bind ViewModel.MyItems}"
ItemTemplate="{StaticResource SomeDataTemplate}"/>
</UserControl>
You can see how the binding is escaping the data template and just targeting a field in code behind. The problem with this is that it’s extremely fragile and inconsistent:
x:Bind
to a property outside the data template works fine ✅x:Bind
to that same property but with function binding fails to build ❌x:Bind
to that same property from a visual state setter crashes at runtime ❌x:Bind
to that same property with a converter crashes at runtime ❌
Steps to reproduce the bug
Steps to reproduce the behavior:
- Download this repro
- Open the solution, run the app
Expected behavior
🤷♂️ 🤷♂️ 🤷♂️
Actual behavior
The binding to the outside field (the view model) works just fine
Screenshots
Windows 10 version | Saw the problem? |
---|---|
Insider Build (xxxxx) | |
November 2019 Update (18363) | Yes |
May 2019 Update (18362) | Yes |
October 2018 Update (17763) | |
April 2018 Update (17134) | |
Fall Creators Update (16299) | |
Creators Update (15063) |
Device form factor | Saw the problem? |
---|---|
Desktop | Yes |
Mobile | |
Xbox | |
Surface Hub | |
IoT |
cc. @MikeHillberg
Issue Analytics
- State:
- Created 3 years ago
- Reactions:12
- Comments:31 (13 by maintainers)
Top Results From Across the Web
How to Bind to Oustide of x:DataType in DataTemplate?
I have a DataTemplate with x:DataType="myModel" now i want to bind to property outside of DataType How can i do this?
Read more >Tutorial: Create data bindings - UWP applications
x:Bind expressions outside of data templates are always bound to the page itself. This means you can reference anything you put in ...
Read more >XML queries and constraints, containment and reformulation
If any variables of the XBind query Xb are bound to element nodes, then Xb cannot be reformulated against the storage schema S...
Read more >Release notes for AWS SCT - AWS Schema Conversion Tool
Resolved an issue where a statement outside the BEGIN/END block was incorrectly converted. Netezza. Amazon Redshift. Improved conversion of the TIME data type ......
Read more >How to Bind to a DataTemplate in UWP
First and foremost, you can't bind to the outside context of a DataTemplate when using x:Bind , even so it is highly recommended...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Seems related to #2237 as well?
My latest trick is to do something like this:
But x:Bind should just let us break into the different scopes we need:
I think that’d cover the majority of scenarios I ever need when dealing with templating. The compiler should just be able to walk the scope up if it’s not finding the reference path at each tier.
Bumping this (see https://github.com/microsoft/microsoft-ui-xaml/discussions/8638).