WPF tip : Using a custom title bar

While you can always custom paint the windows title bar, in most cases it ends up being a very messy approach with possible side effects. What I do in WPF is to disable the title bar in the window by setting its WindowStyle to none.

<Window x:Class="..."
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    ...
    MinWidth="700" WindowStyle="None" Icon="...">

I then define a user control that will act as the title bar and place it on top of the window. Adding maximize/restore, minimize, and close buttons is not a biggie. You just need to remember to toggle the maximize button with restore (to match normal windows behavior). All you need to do is to set WindowState to WindowState.Minimized, WindowState.Normal, or WindowState.Maximized as required. You might also want to handle double-click and route it to the maximize/restore handler (again to match windows behavior). The one final step would be to handle title bar based drag. To do that, handle the MouseDown event and do this :-

void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
    if(e.ChangedButton == MouseButton.Left)
        Application.Current.MainWindow.DragMove();
}
Advertisements

3 thoughts on “WPF tip : Using a custom title bar

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s