Can't bind properties to a textblock

黎 林 0 Reputation points
2023-04-09T07:29:20.6633333+00:00

This is what I wrote in XAML

<TextBlock Text="{x:Bind Thisuser.username,Mode=TwoWay}" FontSize="24" Margin="2,0,0,12"/>

And this is what I wrote in .h

namespace winrt::App3::implementation
{
    class userinfo
    {
    public:
        hstring username;
        bool ifadmin;
    };

    struct MainWindow : MainWindowT<MainWindow>
    {
        MainWindow();
 
public: userinfo Thisuser;

        int32_t MyProperty();
        void MyProperty(int32_t value);

    };
}

When I build the project, it said that Error XLS0432 The property 'Thisuser' was not found in type 'MainWindow'. I'm using C++ so I got few tutorial. I wonder how to fix this prob, thanks !

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
730 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,557 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 32,466 Reputation points Microsoft Vendor
    2023-04-21T02:31:32.3+00:00

    Hello,

    Welcome to Microsoft Q&A!

    You might need to create a class for the user object and make sure that you've decalred the property in the mainWindow's idl.

    For more information, lease take a look at this document: XAML controls; bind to a C++/WinRT property It contains complete steps about how to implement a xaml binding. This is a UWP document but this also works for winui3 c++/winrt project.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments