Lesson 04.04

Create Custom Header in WPF Forms

...

Lesson 04.04

Create Custom Header in WPF Forms

...

Summary (Will be Updated)

Building a Custom Header in WPF with XAML and Python: A Step-by-Step Guide

Welcome back, everyone! In this blog post, we'll dive into the final touches for our WPF form and focus specifically on creating a custom header. This lesson will walk you through rebuilding the header from scratch, adding functionality and styling, and integrating the header with Python event handlers. So, let's get started and see how we can make our header look and feel polished.

1. Setting Up the Header in XAML

Our custom header needs to be responsive, draggable, and have a close button. Here’s the initial setup in XAML:

<Grid Height="22" Background="{StaticResource bg_header}" MouseDown="UIe_header_drag"> 
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="60" />
        <ColumnDefinition/>
        <ColumnDefinition Width="60"/>
    </Grid.ColumnDefinitions>

    <TextBlock Grid.Column="0" Margin="5,0,0,0" VerticalAlignment="Center">
        <Hyperlink Foreground="{StaticResource white}"
                   RequestNavigate="UIe_HyperLinkNavigate"
                   NavigateUri="https://kudos.learnrevitapi.com/r/course">
            EF-Tools.
        </Hyperlink>
    </TextBlock>

    <TextBlock x:Name="UI_Title" Text="__title__" Grid.Column="1"
               HorizontalAlignment="Center"
               Foreground="{StaticResource gray}"
               FontWeight="Regular" Margin="0"
               VerticalAlignment="Center"/>

    <Button Grid.Column="2" Content="X" 
            Width="16" Height="16" FontSize="10"
            HorizontalAlignment="Right"
            Margin="0,0,5,0" 
            Click="UIe_btn_close"/>

This setup establishes a simple three-column grid. The first column contains a hyperlink for branding or navigation, the middle column is for the title, and the right column holds a close button. The header’s styling is controlled by resources like bg_header, white, and gray.

2. Creating Interactivity with Python Events

To add functionality to the header, we define three main event handlers in Python:

  1. Drag Event for moving the header.

  2. Close Button to close the form.

  3. Hyperlink Click to open a website in a browser.

Below is the Python code to handle these events:



The UIe_btn_close method closes the form when the "X" button is clicked. The UIe_header_drag method enables dragging functionality for the header, allowing users to move the form around the screen. The UIe_HyperLinkNavigate method opens the specified URL when the hyperlink is clicked.

3. Adding Styling Resources

To make the header visually appealing, we use custom colors and styles. The XAML below defines various resources, including gradients and color styles:

<Window.Resources>
    <SolidColorBrush x:Key="accent" Color="#ff9a2e"/>
    <SolidColorBrush x:Key="hover" Color="#ff7a2e"/>
    <SolidColorBrush x:Key="white" Color="#f2f2f2"/>
    <SolidColorBrush x:Key="gray" Color="#999999"/>
    <SolidColorBrush x:Key="bg1" Color="#1c1c1c"/>
    <SolidColorBrush x:Key="bg2" Color="#2b2b2b"/>
    <SolidColorBrush x:Key="bg_header" Color="#2c2c2c"/>

These colors help unify the form’s look, and specific elements can inherit these color resources, such as the header’s background (bg_header) and the hyperlink text (white).

4. Creating a Reusable Footer

At the bottom, we’ll add a footer using similar techniques, with a hyperlink for additional branding. Here’s the XAML setup:

<StackPanel Background="{StaticResource bg_header}" Height="20">
    <TextBlock>
        <Hyperlink RequestNavigate="UIe_HyperLinkNavigate"
                   NavigateUri="https://learnrevitapi.com/ef-tools">
            <Image x:Name="UI_footer_logo" Height="18" />
        </Hyperlink>
    </TextBlock>
</StackPanel>

In Python, the image path for the logo is provided dynamically using the BitmapImage class, ensuring it’s portable across different machines.

5. Homework: Customizing Your Header and Footer

Take what you’ve learned here and personalize your header and footer! Try adding animations, different images, or even playing with colors. Share your results in our community, and let’s see how far you can take these styling techniques!

By the end of this post, you’ve learned how to create a fully customizable, interactive header for your WPF forms in Python. You can now apply similar techniques to add unique headers and footers across your forms, creating a more cohesive and professional look. In the next post, I’ll show you how to reuse these styles across multiple forms efficiently.

🙋‍♂️ See you in the next lesson.
- EF

🙋‍♂️ See you in the next lesson.
- EF

Discuss the lesson:

P.S. Sometimes this chat might experience connection issues.
Please be patient or join via Discord app so you can get the most out of this community and get access to even more chats.

Discuss the lesson:

P.S. Sometimes this chat might experience connection issues.
Please be patient or join via Discord app so you can get the most out of this community and get access to even more chats.