Windows Presentation Foundation (WPF) is a framework for building rich user interfaces on Microsoft’s .NET platform. WPF provides flexible and powerful UI capabilities to address various application requirements. In this course, we will detail how to create a menu page using WPF and practice with code examples.

1. Basic Understanding of WPF

WPF provides a declarative UI programming model and uses XAML (Extensible Application Markup Language) to define UI elements. WPF supports the MVVM (Model-View-ViewModel) pattern, enhancing code maintainability and separating UI from business logic. In this course, we will design and implement a menu page based on these core concepts of WPF.

2. Creating a Project

First, let’s create a new WPF application project using Visual Studio.

  1. Open Visual Studio and select ‘Create a new project’.
  2. Select WPF Application and name the project ‘MenuPageApp’.
  3. Once the project is created, open the default MainWindow.xaml file to modify the UI.

3. UI Design

The menu page is a basic interface that provides access to the main functionalities of the application. We will design a menu page that includes the following elements:

  • Menu Items
  • Menu Header
  • Highlight for selected items

3.1 Defining UI with XAML

Below is the code that defines the menu UI in MenuPage.xaml:

        
<Window x:Class="MenuPageApp.MenuPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Menu Page" Height="300" Width="300">
    <Grid>
        <StackPanel>
            <TextBlock Text="Menu" FontSize="24" FontWeight="Bold" HorizontalAlignment="Center" Margin="10"/>
            <Button Content="Option 1" Margin="10" Click="Option1_Click"/>
            <Button Content="Option 2" Margin="10" Click="Option2_Click"/>
            <Button Content="Option 3" Margin="10" Click="Option3_Click"/>
        </StackPanel>
    </Grid>
</Window>
        
    

4. Event Handlers and Business Logic

We need to add code to handle click events for each menu item. This will be done in MenuPage.xaml.cs.

        
using System.Windows;

namespace MenuPageApp
{
    public partial class MenuPage : Window
    {
        public MenuPage()
        {
            InitializeComponent();
        }

        private void Option1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Option 1 selected");
        }

        private void Option2_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Option 2 selected");
        }

        private void Option3_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Option 3 selected");
        }
    }
}
        
    

5. Improving Styles and Layout

We can enhance the UI to make it more visually appealing and improve the user experience. For example, adding hover effects and animations to the buttons can provide visual feedback when the user hovers over menu items.

5.1 Defining Styles

Button styles can be defined in the App.xaml file:

        
<Application x:Class="MenuPageApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MenuPage.xaml">
    <Application.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="LightGray"/>
            <Setter Property="Margin" Value="5"/>
            <Setter Property="Padding" Value="10"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="DarkGray"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Application.Resources>
</Application>
        
    

6. Complete Code

Now, let’s organize the complete code to finalize the entire application.

6.1 MenuPage.xaml

        
<Window x:Class="MenuPageApp.MenuPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Menu Page" Height="300" Width="300">
    <Grid>
        <StackPanel>
            <TextBlock Text="Menu" FontSize="24" FontWeight="Bold" HorizontalAlignment="Center" Margin="10"/>
            <Button Content="Option 1" Click="Option1_Click"/>
            <Button Content="Option 2" Click="Option2_Click"/>
            <Button Content="Option 3" Click="Option3_Click"/>
        </StackPanel>
    </Grid>
</Window>
        
    

6.2 MenuPage.xaml.cs

        
using System.Windows;

namespace MenuPageApp
{
    public partial class MenuPage : Window
    {
        public MenuPage()
        {
            InitializeComponent();
        }

        private void Option1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Option 1 selected");
        }

        private void Option2_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Option 2 selected");
        }

        private void Option3_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Option 3 selected");
        }
    }
}
        
    

6.3 App.xaml

        
<Application x:Class="MenuPageApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MenuPage.xaml">
    <Application.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="LightGray"/>
            <Setter Property="Margin" Value="5"/>
            <Setter Property="Padding" Value="10"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="DarkGray"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Application.Resources>
</Application>
        
    

7. Conclusion

In this course, we learned how to create a menu page using WPF. The menu page is an important part of the user interface, providing access to various functions of the application. We defined the UI using XAML and structured the basic framework to respond to user interactions by handling events with C#. We also explored how to improve user experience through styling.

By applying these fundamentals, you can implement complex UIs and various business logic in WPF applications. We hope this serves as a stepping stone for implementing more advanced features and designs. If you have any additional questions, feel free to ask at any time.