C
C#2y ago
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 and 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 doesn't have these properties. How would I do that same thing in WinUI 3?
5 Replies
Klarth
Klarth2y ago
What properties? Title? It's not bindable in WinUI3 unless it was changed very recently in the newest version. You need to be specific when describing errors. Also, you should be using x:Bind in WinUI3 instead of Binding once you get things under control.
Turwaith
TurwaithOP2y ago
I am talking about Window.DataContext and <Window.Resources>
Klarth
Klarth2y ago
Well, the short answer for that is that you need to find another approach because Window isn't a FrameworkElement in WinUI3/UWP, so it doesn't have those properties.
Jaiganésh
Jaiganésh2y ago
You need to put it inside the root content (the grid in this case).
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server