"If I change UI frameworks, will the C# MVVM backend of my application still work?" This is the most frequent question that we've received from developers since releasing the Qt Bridge for C#. We wanted to provide some detailed information to back the answer to that question, which is: yes, when moving to a Qt Quick / QML UI, your C# MVVM backend will still work in the exact same way. As we will see in the course of this blog post, this is due to two major factors: a) how C# and Qt Quick are brought together by the C# bridge, and b) the intrinsic design principles promoted by MVVM.
In the context of application development with C#, Model–View–ViewModel (MVVM) remains highly relevant as the preferred design pattern for implementing a clear separation of concerns between business logic (the Model), presentation logic (the ViewModel) and user interface (the View). Since its introduction more than 20 years ago, MVVM in C# has kept pace with the evolution of application development, while retaining key traits such as a declarative UI specification, data bindings and state-based view composition. Qt Quick shares these same traits, making it well suited to fill in the View role in an MVVM-enabled C# application.

Qt Quick in Modern MVVM C# Applications
Modern MVVM applications are typically designed around three key principles: separation of concerns, dependency injection, and declarative view composition. Presentation state and UI behavior reside in ViewModels, user interface rendering is handled by Views, and application services are supplied through a Dependency Injection (DI) container. Much of the boilerplate traditionally associated with MVVM is simplified through the use of support libraries, such as the CommunityToolkit.Mvvm library, that provide implementations of common MVVM patterns like observable properties and relay commands.
A ViewModel-First strategy is commonly used when connecting the ViewModel and View layers. During startup, the application sets up a host and configures its DI container. Core services, navigation services, and ViewModels are registered first. The main View is then created, receiving its required dependencies through constructor injection, including the ViewModel representing the application's initial state.
When using Qt Quick instead of WPF to implement the View layer, all of the above-mentioned mechanisms are preserved, requiring only the following two adjustments:
- Startup and shutdown code runs in the context of the application's
Main instead of the OnStartup and OnExit event handlers.
- The main View is created by calling the
Qml.Load function. The initial value of any required properties can be passed using an appropriate initialization object, accomplishing the same effect as constructor injection.
| WPF |
|
Qt Quick |
private ApplicationHost applicationHost;
private async void OnStartup( object sender, StartupEventArgs e) { applicationHost = ApplicationHost.Create(e.Args); await applicationHost.StartAsync(); var mainViewModel = applicationHost .Services.GetRequiredService<MainViewModel>();
MainWindow = new MainWindow(mainViewModel); MainWindow.Show();
await mainViewModel.InitializeAsync(); }
private async void OnExit( object sender, ExitEventArgs e) { if (applicationHost is null) return; using var timeout = new CancellationTokenSource( TimeSpan.FromSeconds(5)); try { await applicationHost.StopAsync(timeout.Token); } finally { await applicationHost.DisposeAsync(); } } |
|
internal static async Task Main(string[] args) { var applicationHost = ApplicationHost.Create(args); await applicationHost.StartAsync(); var mainViewModel = applicationHost .Services.GetRequiredService<MainViewModel>();
Qml.Load(new MainWindowArgs() { ViewModel = mainViewModel });
await mainViewModel.InitializeAsync();
Qml.WaitForExit();
using var timeout = new CancellationTokenSource( TimeSpan.FromSeconds(5)); try { await applicationHost.StopAsync(timeout.Token); } finally { await applicationHost.DisposeAsync(); } } |
MVVM C# Example: Application startup and shutdown in WPF vs Qt Quick.
From the above code sample, we can see that, when moving from WPF to Qt Quick, most of the application startup and shutdown code stays exactly the same. In the Qt Quick case, the code is fully enclosed in the application's Main method, whereas, with WPF, the code needs to be split between two event handlers, requiring an additional field to maintain the host context between startup and shutdown.
Selecting the View: ContentControl Alternatives in QML
Following the same ViewModel-First principle described above, navigation throughout the application is driven entirely by ViewModel state. A View becomes active only when the presentation logic layer decides that a matching ViewModel should be displayed. For example, the main ViewModel might delegate presentation management to several component ViewModels, exposing the currently active component through a Current observable property. In WPF, this property would typically be referenced in a XAML binding used in a ContentControl within the main View. The ContentControl would then display its contents according to the type of the component ViewModel set as Current. This allows the application to switch between Views simply by changing the selected ViewModel.
When moving to Qt Quick / QML, no changes are required on the part of the ViewModel layer. There are several options in Qt Quick for selecting which View to use for rendering a particular ViewModel, such as the Loader, StackView or StackLayout object types. The simplest option is to create the Views within a StackLayout object, and then select the active View through a property binding linking the currentIndex to the type of the Current ViewModel. This effectively emulates the same behavior as WPF’s ContentControl.
| WPF |
|
Qt Quick |
<Application x:Class="PeopleDirectory.AppWpf" … xmlns:vm="clr-namespace:PeopleDirectory.Core.ViewModels; assembly=PeopleDirectory.Core" xmlns:views="clr-namespace:PeopleDirectory.Views" Startup="OnStartup" Exit="OnExit"> <Application.Resources> <ResourceDictionary> … <DataTemplate DataType="{x:Type vm:LoadingViewModel}"> <views:LoadingView /> </DataTemplate> <DataTemplate DataType="{x:Type vm:UsersViewModel}"> <views:UsersView /> </DataTemplate> <DataTemplate DataType="{x:Type vm:UserDetailsViewModel}"> <views:UserDetailsView /> </DataTemplate> </ResourceDictionary> </Application.Resources> </Application>
<Window x:Class="PeopleDirectory.Views.MainWindow" … <ContentControl Content="{Binding Current}" /> … </Window> |
|
StackLayout { … LoadingView { id: loadingView animateIndicator: StackLayout.isCurrentItem }
UsersView { id: usersView viewModel: mainWindow.viewModel.current }
UserDetailsView { id: userDetailsView viewModel: mainWindow.viewModel.current }
currentIndex: switch (viewModel.current.getType()) {
case ViewModelTypes.loadingViewModel: return loadingView.StackLayout.index
case ViewModelTypes.usersViewModel: return usersView.StackLayout.index
case ViewModelTypes.userDetailsViewModel: return userDetailsView.StackLayout.index
default: return -1 } } |
Comparing XAML (WPF) and QML (Qt Quick): selecting which View to display according to the currently active ViewModel.
The ViewModel layer requires no changes when moving from WPF to Qt Quick. In both cases, the selection logic is bound to the Current ViewModel component and maps it to an appropriate View. In the above example, the WPF implementation uses a common pattern of encoding the ViewModel-to-View mapping in the application’s global resource dictionary. In the Qt Quick implementation, the mapping and selection logic are fully described locally in the main View, without any external or hidden dependency. This makes for a more readable and maintainable UI specification.
Displaying Collections: WPF DataTemplate vs QML Delegate
WPF and Qt Quick have similar approaches when it comes to displaying the items of a collection. In both frameworks, the control responsible for the rendering – for example, a ListBox in WPF or a ListView in Qt Quick – is bound to the collection object to display. In WPF, the collection is bound to the ItemsSource property of the control, whereas in Qt Quick it is bound to the model property. The C# Bridge ensures that most collection types are compatible and can thus be directly bound to the model property. This includes the ObservableCollection type, which is commonly used in the context of MVVM in C#. This means that, again, no changes are required at the ViewModel level in order to switch the UI specification from WPF / XAML over to Qt Quick / QML.
For rendering the collection items, both frameworks also have similar mechanisms involving a generic component View that is applied to each of the items and “knows” how to display them. In WPF, this component is specified as a DataTemplate, while in Qt Quick it is specified as a delegate component, set as the value of the delegate property on the rendering control. Within a QML delegate component, the item contract is clearly specified as a list of required properties, with the item property providing access to the item itself.
| WPF |
|
Qt Quick |
<ListBox … <ListBox.ItemsSource> <MultiBinding Converter="{StaticResource LoadingItemsSourceConverter}"> <Binding Path="FilteredUsers" /> <Binding Path="IsLoading" /> </MultiBinding> </ListBox.ItemsSource> … <ListBox.ItemTemplate> <DataTemplate> <Button Command="{Binding DataContext.OpenDetailsCommand, RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding}" Style="{StaticResource CardButton}"> <Grid> … <Border Background="{Binding AvatarColorIndex, Converter={StaticResource AvatarColorConverter}}"> … </Border> … </Grid> </Button> </DataTemplate> </ListBox.ItemTemplate> </ListBox> |
|
ListView { … model: !viewModel.isLoading ? viewModel.filteredUsers : []
delegate: AppStyle.CardButton { id: user required property var item required property string fullName required property string email required property string initials required property int avatarColorIndex
onClicked: viewModel.openDetails(item)
RowLayout { … Rectangle { color: AppStyle.Theme.colors .avatarColor(user.avatarColorIndex) … } … } } }
|
Example: Displaying collection items in XAML (WPF) vs QML (Qt Quick).
In the above sample code, the FilteredUsers property is bound to the control responsible for displaying that collection. However, the binding needs to be conditional on the value of another property, IsLoading, such that, when that property has a value of true, no items of the collection are to be displayed. In WPF, this requires the introduction of a converter, defined in a separate C# module, and that will implement the conditional behavior of the XAML data binding. In Qt Quick / QML, the same behavior is encoded directly in a one-line binding expression. Similarly, while in WPF, the reaction to the user pressing an item is given as a XAML binding to a command and requires an additional attribute for the command’s parameter, in QML, the same reaction is specified with a simple function-call expression.
Another notable difference pertains to how control customization works in WPF and Qt Quick. Like in the above example, where a standard Button is customized with a Style, WPF commonly expresses control customization through setter-based styles and control templates resolved from resource dictionaries. Conversely, Qt Quick / QML allows customizing controls more naturally, as new types that specialize and compose existing controls. In the above example, instead of using a standard control, a custom control type is used (CardButton) which inherits from and extends the standard Qt Quick Button. This more object-oriented approach to customization promotes discoverability (CardButton signals a specific intent at the usage site) and encapsulation (implementation details are contained within the CardButton definition).
MVVM C# Example: WPF and Qt Quick Side by Side
The code samples found throughout this blog post are part of a fully functional MVVM C# app example implemented both with WPF and with Qt Quick. The two implementations share a core library containing the Model and ViewModel layers. The core library works with both WPF and Qt Quick implementations without requiring any changes.

MVVM C# Example: GUI of the People Directory sample application.
The source code for the example can be found at: github.com/qt/qtbridge-csharp-examples
Conclusion: New UI, Same C# MVVM Backend
In this blog post, we showed how to move an application’s UI from WPF to Qt Quick / QML without major changes to the C# MVVM backend:
- The View layer is specified in QML instead of XAML.
- The Model and ViewModel layers remain unchanged.
- Startup and shutdown code require minor changes.
Why Change from XAML to QML
There are several reasons to move from a traditional XAML-based UI framework to Qt Quick / QML, including:
- Use an expressive UI specification language instead of constantly battling against the limitations of an XML-based language.
- Define data bindings with conventional expressions, including arithmetic and logical operators, instead of having to use converters even for simple scenarios like
InverseBooleanConverter, NullToVisibilityConverter or StringNullOrEmptyToVisibilityConverter.
- Directly call functions of the ViewModel’s public API instead of relying on the additional concept of Command.
- Where necessary and appropriate, define behavior using inline imperative code instead of code-behind modules.
- Create scoped object hierarchies instead of a “flat” application-wide resource dictionary.
- Customize controls through object inheritance and composition instead of setter-based styles.
What's Next?
If you haven’t already, build your C# GUI with the Qt Bridge and share your feedback.
Here are some links that you may find useful:
Your feedback during the Technology Preview helps us most. Tell us what works, what's missing and what gets in your way:
We look forward to seeing what you build!