question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

x:Bind outside data templates is extremely inconsistent

See original GitHub issue

Describe 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:

  1. Download this repro
  2. Open the solution, run the app

Expected behavior

🤷‍♂️ 🤷‍♂️ 🤷‍♂️

Actual behavior

The binding to the outside field (the view model) works just fine

Screenshots

image

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:open
  • Created 3 years ago
  • Reactions:12
  • Comments:31 (13 by maintainers)

github_iconTop GitHub Comments

8reactions
michael-hawkercommented, Jan 14, 2022

Seems related to #2237 as well?

My latest trick is to do something like this:

<Page x:Name="ThisPage"...>
   <ItemsControl>
       <ItemsControl.ItemTemplate>
           <DataTemplate x:DataType="local:MyType">
               <Button Command="{Binding ViewModel.SaveCommand, ElementName=ThisPage}" CommandParameter="{x:Bind (local:MyType)}"/>

But x:Bind should just let us break into the different scopes we need:

  • Local (data item itself)
  • Container (ListViewItem for instance)
  • “Parent” (ListView for instance)
  • “Global” (i.e. Page)

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.

7reactions
Sergio0694commented, Jul 13, 2023
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found