© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
6 replies
Turwaith

❔ Porting a mvvm based WPF application to WinUI 3

I am trying to rewrite a WPF application that is based on the mvvm pattern into WinUI 3. Now I already fail at the MainWindow.
In WPF, I usually don't have any content in the MainWindow, I only use it to fill in the views I then fill with content. I use
Window.DataContext
Window.DataContext
and
Window.Resources
Window.Resources
for that, as follows:

<Window x:Class="MyProject.Client.ClientUI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:viewmodels="clr-namespace:MyProject.Client.ClientUI.ViewModels"
        xmlns:views="clr-namespace:MyProject.Client.ClientUI.Views"
        xmlns:local="clr-namespace:MyProject.Client.ClientUI"
        mc:Ignorable="d"
        Title="{Binding Name}">

    <Window.DataContext>
        <viewmodels:MainViewModel />
    </Window.DataContext>

    <Window.Resources>
        <DataTemplate DataType="{x:Type viewmodels:DisplayViewModel}">
            <views:DisplayView />
        </DataTemplate>
    </Window.Resources>

    <Grid>

        <ContentControl Content="{Binding CurrentViewModel}" />
    </Grid>
</Window>
<Window x:Class="MyProject.Client.ClientUI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:viewmodels="clr-namespace:MyProject.Client.ClientUI.ViewModels"
        xmlns:views="clr-namespace:MyProject.Client.ClientUI.Views"
        xmlns:local="clr-namespace:MyProject.Client.ClientUI"
        mc:Ignorable="d"
        Title="{Binding Name}">

    <Window.DataContext>
        <viewmodels:MainViewModel />
    </Window.DataContext>

    <Window.Resources>
        <DataTemplate DataType="{x:Type viewmodels:DisplayViewModel}">
            <views:DisplayView />
        </DataTemplate>
    </Window.Resources>

    <Grid>

        <ContentControl Content="{Binding CurrentViewModel}" />
    </Grid>
</Window>


Now this seems not to work anymore in WinUI 3, as VS keeps telling me that
Window
Window
doesn't have these properties. How would I do that same thing in WinUI 3?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Embedding an application WinUI 3
C#CC# / help
4y ago
❔ WinUI MVVM: Dealing with enums
C#CC# / help
3y ago
WPF Question MVVM
C#CC# / help
2y ago
✅ wpf json CRUD(mvvm)
C#CC# / help
2y ago