WinUI 3 Form Example: Build Stunning UIs Fast

WinUI 3, the latest version of Windows UI Library, offers developers an extensive range of tools to create modern, responsive, and visually appealing Windows apps. One of the standout features of WinUI 3 is its support for XAML and C++ or C# programming languages, making it a powerful choice for UI development. Let's explore a practical example of creating a form using WinUI 3.

WPF vs WinForms - Making the Right Decision in 2026 - NDepend Blog
WPF vs WinForms - Making the Right Decision in 2026 - NDepend Blog

Before diving into the form example, it's important to have WinUI 3 set up correctly. This typically involves installing the Windows App SDK, which includes WinUI 3, and creating a new WinUI project in your preferred IDE, such as Visual Studio. Once your development environment is configured, you're ready to start building your WinUI 3 form.

a form with the name and number on it
a form with the name and number on it

Creating a Basic WinUI 3 Form

To create a basic form in WinUI 3, you'll need to follow several steps. This process involves adding the necessary namespaces, designing the form's UI in XAML, and implementing any required functionality in code-behind.

WinForms Resize Controls | ComponentOne
WinForms Resize Controls | ComponentOne

Let's break down this process into manageable tasks, starting with the basic structure of a WinUI 3 form.

Setting Up the Project and Adding Namespaces

a screen shot of the welcome page for someone's social wi - fi account
a screen shot of the welcome page for someone's social wi - fi account

First, ensure that your project's target framework is set to 'WindowsApp' or higher. This ensures that you're targeting the correct version of WinUI 3. Next, add the necessary namespaces to your code-behind file. For a basic form, you'll need to include the following using directives:

<using namespace:Microsoft.UI.Xaml>
<using namespace:Microsoft.UI.Xaml.Controls>
<using namespace:Microsoft.UI.Xaml.Navigation>

These namespaces provide access to essential WinUI 3 controls and functionality.

Designing the Form's UI with XAML

Temporary Will Form Sample Template | Buy Sample Forms Online
Temporary Will Form Sample Template | Buy Sample Forms Online

With the project set up and namespaces defined, it's time to design the UI of your form using XAML. In the MainWindow.xaml file, you'll replace the existing Grid with a StackPanel, which provides a more intuitive layout for forms. Here's a simple example of a WinUI 3 form's XAML structure:


<Grid>
    <StackPanel>
        <TextBlock Text="Welcome to my WinUI 3 Form!" FontSize="24" HorizontalAlignment="Center" Margin="0,10" />
        <TextBox Width="300" Height="30" Margin="0,10" />
        <Button Content="Submit" Width="100" Height="30" Click="Button_Click" />
    </StackPanel>
</Grid>

This example creates a simple form with a welcome message, a text box, and a submit button. The Click event of the button is wired up to a method in the code-behind, which we'll implement next.

Implementing Functionality in Code-Behind

Free 22+ HTML5 Signup & Registration Forms - HTML, CSS
Free 22+ HTML5 Signup & Registration Forms - HTML, CSS

The code-behind file (MainWindow.xaml.cs) is where you'll add your functionality to interact with the controls created in the XAML. In our example, we'll handle the Click event of the submit button. Here's a simple implementation:

private void Button_Click(object sender, RoutedEventArgs e)

an application for medical discharge form is shown in black and white, with the name
an application for medical discharge form is shown in black and white, with the name
an image of a website page with the wordpress theme highlighted in blue and green
an image of a website page with the wordpress theme highlighted in blue and green
the login screen for an email form is shown in this screenshote image
the login screen for an email form is shown in this screenshote image
How To Create Responsive Registration Form in HTML & CSS
How To Create Responsive Registration Form in HTML & CSS
the clean web forms page is displayed in purple and white colors, with two different options for
the clean web forms page is displayed in purple and white colors, with two different options for
an application form is shown in two different colors and font, with the same image on it
an application form is shown in two different colors and font, with the same image on it
Vue.js
Vue.js
Corporate Form Design
Corporate Form Design
P11 Form - Fill Out and Sign Printable PDF Template | airSlate SignNow
P11 Form - Fill Out and Sign Printable PDF Template | airSlate SignNow
Webflow Login Form With Google Auth for Webflow
Webflow Login Form With Google Auth for Webflow
Fillable W2 Form 2024: Printable IRS Template (digital Download) - Etsy
Fillable W2 Form 2024: Printable IRS Template (digital Download) - Etsy
20,000+ Free Online Form Templates & Form Examples | Jotform
20,000+ Free Online Form Templates & Form Examples | Jotform
an image of a computer screen with the same page on it, but different colors and font
an image of a computer screen with the same page on it, but different colors and font
Editable Job Application Form Bundle, Printable Employee Hiring Template, Business Intake Form, PDF & Word Application, Small Business Forms
Editable Job Application Form Bundle, Printable Employee Hiring Template, Business Intake Form, PDF & Word Application, Small Business Forms
Leave Management & Leave Application Form Templates
Leave Management & Leave Application Form Templates
a screen shot of the babysith request form on an iphone with blue and white geometric background
a screen shot of the babysith request form on an iphone with blue and white geometric background
a red document with the words wir ersteen angebote softt,
a red document with the words wir ersteen angebote softt,
a printable release form for the general laborility release form is shown in this image
a printable release form for the general laborility release form is shown in this image
20,000+ Free Online Form Templates & Form Examples | Jotform
20,000+ Free Online Form Templates & Form Examples | Jotform
an image of a website page with the wordpress creation section on top of it
an image of a website page with the wordpress creation section on top of it

To complete this example, you can add a message box that displays the text entered by the user into the text box:

MessageDialog dialog = new MessageDialog(this.TextBox.Text);
await dialog.ShowAsync();

Handling Navigation and Loading Data

WinUI 3 forms also support navigation and data binding, allowing you to create more complex and dynamic user interfaces. To demonstrate this, let's add a simple navigation feature to our form. We'll modify the XAML to include a NavigationView control, which will allow users to switch between different pages:


<NavigationView>
    <NavigationView.MenuItems>
        <NavigationViewItem Content="Home" Icon="" IsSelected="True" />
        <NavigationViewItem Content="About" Icon="" />
    </NavigationView.MenuItems>
    <NavigationView.Content>
        <Frame x:Name="ContentFrame" />
    </NavigationView.Content>
</NavigationView>

Next, in the code-behind, we'll handle the selection changed event to navigate between the different pages:

private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
    if=args.IsSelected
    {
        if (args.SelectedItem as NavigationViewItem == HomeMenuItem)
        {
            ContentFrame.Navigate(typeof(HomePage));
        }
        else if (args.SelectedItem as NavigationViewItem == AboutMenuItem)
        {
            ContentFrame.Navigate(typeof(AboutPage));
        }
    }
}

In this example, we're navigating to two different pages (HomePage and AboutPage) based on the user's selection in the NavigationView. Keep in mind that you'll need to create these pages in your project for this to work correctly.

As you progress with WinUI 3 development, you'll find that the platform provides countless opportunities to create innovative and engaging Windows apps. By understanding how to create forms and handle basic functionality, you're well on your way to mastering WinUI 3. For more advanced topics and examples, be sure to explore the official WinUI 3 documentation and other resources to expand your skills.

Related Articles

W3 Xpath Functions Sql Where With 3 Conditions With Clause Sql W3schools W3schools Xslt W3schools Ms Sql Server How To Use With Clause In Sql Youtube Sql Tutorial For Beginners Rank In Mysql W3schools W 3 Form Example Create User Sql W3schools