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

WinUI 3 GridView ItemTemplate binding not working as expected in Windows Runtime Component library (UWP) used in Project Reunion Packaged Desktop App

Open hereafter opened this issue 5 years ago • 1 comments

Describe the bug

The ItemTemplate of GridView binding not working as expected with follow project reunion setup (project attached at the end).

  1. The app entrance is a C++ Packaged Desktop in WinUI 3 (Project Reunion)
  2. The GridView is in a Windows Runtime Component Library WinUI 3

any string binding shows empty.

Steps to reproduce the bug

Steps to reproduce the behavior:

  1. Create a C++ "App1" project with the "Blank App, Packaged (WinUI3 in Desktop)" template.

  2. Create a C++ "RuntimeComponent1" project with the "Windows Runtime Component (WinUI 3)" template.

  3. In "RuntimeComponent1" project, add a "New Item" "DataEntity2" with "View Model (C++/WinRT)" template. Create a String "Label" property for "DataEntity2“

"DataEntity2.idl"

namespace RuntimeComponent1
{
    [bindable]
    [default_interface]
    runtimeclass DataEntity2 
    {
        DataEntity2();
        String Label;
    }
}

"DataEntity2.h"

#pragma once

#include "DataEntity2.g.h"

namespace winrt::RuntimeComponent1::implementation
{
    using namespace winrt;

    struct DataEntity2 : DataEntity2T<DataEntity2>
    {
        DataEntity2() = default;

        hstring Label();
        void Label(hstring value);


    private:
        hstring _label;
    };
}

namespace winrt::RuntimeComponent1::factory_implementation
{
    struct DataEntity2 : DataEntity2T<DataEntity2, implementation::DataEntity2>
    {
    };
}

"DataEntity2.cpp"


#include "pch.h"
#include "DataEntity2.h"
#if __has_include("DataEntity2.g.cpp")
#include "DataEntity2.g.cpp"
#endif

namespace winrt::RuntimeComponent1::implementation
{
    hstring DataEntity2::Label()
    {
        return _label;
    }

    void DataEntity2::Label(hstring value)
    {
        _label = value;
    }
}

  1. In "RuntimeComponent1" project add a "UserControl" with "UserControl (WinUI 3)" template

  2. In the "UserControl", in XAML create a named grid view "testView" with item template set to a TextBlock , the data type is "DataEntity2" for the data template, the "Text" of TextBlock in data template is binded to "Label" property with {x:Bind Label}


<UserControl
    x:Class="RuntimeComponent1.UserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:RuntimeComponent1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <GridView x:Name="testView">
            <GridView.ItemTemplate>
                <DataTemplate x:DataType="local:DataEntity2">
                    <Grid>
                        <TextBlock Text="{x:Bind Label}" />
                    </Grid>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>
    </Grid>
</UserControl>

  1. In "UserControl" constructor, construct a items source with the following code
auto entities = single_threaded_observable_vector<DataEntity2>();
for (int i = 0; i < 100; i++)
{
     DataEntity2 entity;

     entity.Label(L"Hello");

     entities.Append(entity);

 }
 this->testView().ItemsSource(entities); 
  1. Add Project Reference "RuntimeComponent1" to project "App1"

The build will fail: apply fixes mentioned here #4907

in "pch.h" of App1

Add

#include <winrt/RuntimeComponent1.h>

  1. In "App1" MainPage.xaml use the "UserControl" created in "RuntimeComponent1"

<Window
    x:Class="App1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:ui="using:RuntimeComponent1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <ui:UserControl>
            
        </ui:UserControl>
    </Grid>
</Window>

  1. Build and Run "App1"

Expected behavior

The main window shows a collection of "Hello"s but the app show a collection of empty strings.

Screenshots

image

Version Info

Visual Studio version:

VS 2019: 16.9.4 Project Reunion: 0.5.6 CppWinrt: 2.0.210505.3

NuGet package version:

WinUI 3 - Project Reunion 0.5: 0.5.6 CppWinRT - Microsoft.Windows.CppWinRT 2.0.210505.3

Windows app type:

UWP Win32
Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
October 2020 Update (19042) Yes
May 2020 Update (19041)
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context

App1.ZIP

hereafter avatar May 06 '21 07:05 hereafter

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 15:07 github-actions[bot]