Useful Development

WPF: customizing the appearance of a window

posted on Jun 27, 2019

I don't get much opportunity to work with WPF anymore which has meant I have lots of code going to waste. One piece of code, which I thought I could share, is around customizing the default window (title bar, window border, window buttons, drag, drop and resize). The approach I took was to encapsulate all the code in behaviors and commands and then use a XAML style for the Window. This way everything is reusable. I've put the following behaviors and commands together into a code sample showing how to create a custom window in WPF and published it on MSDN here. The individual commands and behaviors are listed below.

  • ControlDoubleClickBehavior (add a double click command to a control)
  • ShowSystemMenuBehavior (show the system menu on either right click or left click of a control)
  • WindowCloseCommand (close the window on left click of control)
  • WindowDragBehavior (drag window when left click on control)
  • WindowMaximizeCommand (maximize the window)
  • WindowMinimizeCommand (minimize the window)
  • WindowResizeBehavior (resize the window on left click and hold)

To see how to use them have a look at the window template in App.xaml. Please feel free to download the sample and use the code in your own projects.

WPF