microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

x bind function does not work within datatemplate

Open KWodarczyk opened this issue 2 years ago • 8 comments

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

image

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

KWodarczyk avatar Oct 10 '22 19:10 KWodarczyk

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 avatar Oct 10 '22 21:10 MikeHillberg

@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.

KWodarczyk avatar Oct 10 '22 21:10 KWodarczyk

Agree, still a bug, not closing the issue. I was just looking for a workaround in the meantime. Thanks for opening.

MikeHillberg avatar Oct 10 '22 21:10 MikeHillberg

Appreciated.

KWodarczyk avatar Oct 10 '22 21:10 KWodarczyk

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.

github-actions[bot] avatar Jul 29 '23 17:07 github-actions[bot]

Comment

KWodarczyk avatar Jul 29 '23 17:07 KWodarczyk

Related to #2508 maybe too just for x:bind in general?

michael-hawker avatar Sep 18 '23 17:09 michael-hawker

This is still an issue. Just because nobody fixes it, doesn't mean it's not still an issue.

DRAirey1 avatar Feb 21 '24 16:02 DRAirey1