0

I wish to use mvvm instead of hardcoding all the change events in MainWindow. Tried lot of example deom different sources but not working with c++/winrt winui-3....

Main Window

File Mainwindow.xaml

<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="MyBindingExample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyBindingExample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="MyBindingExample">

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBox x:Name="OrderNo_TextBox" Text="{Binding OrderNo, Mode=TwoWay}" Width="100" Height="30" Margin="10"/>
    </StackPanel>
</Window>

File: MainWindow.idl

namespace MyBindingExample
{
    [default_interface]
    runtimeclass MainWindow : Microsoft.UI.Xaml.Window
    {
        MainWindow();
    }
}

File MainWindow.xaml.h

#pragma once

#include "MainWindow.g.h"

namespace winrt::MyBindingExample::implementation
{
    struct MainWindow : MainWindowT<MainWindow>
    {
        MainWindow();

        MyBindingExample::DocumentViewModel DocViewModel() const;

    private:
        MyBindingExample::DocumentViewModel m_viewModel{ nullptr };
    };
}

namespace winrt::MyBindingExample::factory_implementation
{
    struct MainWindow : MainWindowT<MainWindow, implementation::MainWindow>
    {
    };
}

File: MainWindow.xaml.cpp

#include "pch.h"

#include "MainWindow.xaml.h"
#if __has_include("MainWindow.g.cpp")
#include "MainWindow.g.cpp"
#endif

using namespace winrt;
using namespace Microsoft::UI::Xaml;

namespace winrt::MyBindingExample::implementation
{

    MainWindow::MainWindow()
    {
        InitializeComponent();
        m_viewModel = MyBindingExample::DocumentViewModel();
        m_viewModel.OrderNo(L"12345");
    }
    MyBindingExample::DocumentViewModel MainWindow::DocViewModel() const
    {
        return MyBindingExample::DocumentViewModel();
    }
}

View Model

File: DocumentViewModel.idl

namespace MyBindingExample
{
    [bindable]
    [default_interface]
    runtimeclass DocumentViewModel
    {
        DocumentViewModel();
        String OrderNo;
    }
}

File: DocumentViewModel.h

#pragma once

#include "DocumentViewModel.g.h"
#include "winrt/base.h"

namespace winrt::MyBindingExample::implementation
{
    struct DocumentViewModel : DocumentViewModelT<DocumentViewModel>
    {
        DocumentViewModel() = default;

        hstring OrderNo();
        void OrderNo(hstring value);

    private:
        hstring m_orderNo;
    };
}

namespace winrt::MyBindingExample::factory_implementation
{
    struct DocumentViewModel : DocumentViewModelT<DocumentViewModel, implementation::DocumentViewModel>
    {
    };
}

File: DocumentViewModel.cpp

#include "pch.h"

#include "DocumentViewModel.h"
#if __has_include("DocumentViewModel.g.cpp")
#include "DocumentViewModel.g.cpp"
#endif
#include "winrt/base.h"

namespace winrt::MyBindingExample::implementation
{
    hstring DocumentViewModel::OrderNo()
    {
        return m_orderNo;
    }

    void DocumentViewModel::OrderNo(hstring orderNo)
    {
        m_orderNo = orderNo;
    }
}
2
  • When you change a property such as OrderNo you must raise a PropertyChanged event learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/… Commented Nov 22, 2024 at 12:49
  • I don’t see anything in there that sets the source of the Binding. Suggest you use x:Bind instead, then the binding path is relative to the window or page. You may also want a Frame and a Page in there. Commented Dec 31, 2024 at 22:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.