microsoft-ui-xaml
microsoft-ui-xaml copied to clipboard
x bind function does not work within datatemplate
Describe the bug
x bind function does not work within datatemplate xaml
<Page
x:Class="WinUI3DesktopMVVM.Views.LayoutSandBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinUI3DesktopMVVM.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:staticStuff ="using:WinUI3DesktopMVVM"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<DataTemplate x:Key="myTemplate">
<TextBlock Text="{x:Bind staticStuff:StaticTestClass.GetString()}"/>
</DataTemplate>
</Page.Resources>
<StackPanel Background="DarkCyan">
<ContentControl ContentTemplate="{StaticResource myTemplate}"/><!--does not work-->
<ContentControl Content="{x:Bind staticStuff:StaticTestClass.GetString()}"/> <!--works fine-->
</StackPanel>
</Page>
code
namespace WinUI3DesktopMVVM
{
public class StaticTestClass
{
public static string StaticSTring { get; set; } = "TESTing stirng";
public static string GetString()
{
return "TEST_string";
}
}
}
Steps to reproduce the bug
just copy paste this code in winui3 app
Expected behavior
expected string to be printed twice but it's printed only once
Screenshots
NuGet package version
WinUI 3 - Windows App SDK 1.1.5
Windows app type
- [ ] UWP
- [X] Win32
Device form factor
Desktop
Windows version
Windows 11 (21H2): Build 22000
Additional context
No response
x:Bind generates code, and that code in this case is noop'ing the DataTemplate case because the item value is null (the Content property of the first ContentControl). Likely a performance optimization but looks to me like an ill-advised one.
You can work around it by giving that ContentControl some content. Then, though it gets confused about the DataTemplate not having an x:DataType. So both changes looks like:
<Page.Resources>
<DataTemplate x:Key="myTemplate" x:DataType="x:String"> <!-- Set DataType -->
<TextBlock Text="{x:Bind staticStuff:StaticTestClass.GetString()}"/>
</DataTemplate>
</Page.Resources>
<StackPanel Background="DarkCyan">
<ContentControl ContentTemplate="{StaticResource myTemplate}" Content="Test" /> <!-- Set Content -->
<ContentControl Content="{x:Bind staticStuff:StaticTestClass.GetString()}"/>
</StackPanel>
@MikeHillberg wow giving ContentControl some dummy content and adding x:DatatType fixes the issue <ContentControl Content=" " ContentTemplate="{StaticResource myTemplate}"/>
that is redonkulous. But Thanks. Still a bug though.
Agree, still a bug, not closing the issue. I was just looking for a workaround in the meantime. Thanks for opening.
Appreciated.
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.
Comment
Related to #2508 maybe too just for x:bind in general?
This is still an issue. Just because nobody fixes it, doesn't mean it's not still an issue.