Moving a WPF Window with Custom Chrome
Join the DZone community and get the full member experience.Join For Free
So let’s have at it:
We’ll create a simple window with no built-in border or titlebar. We’ll use a border, some shapes and a HeaderedContentControl to provide stand-in titlebar/content areas.
<!-- New Title Bar -->
<!-- If you want to move using something else, wire up the event there instead -->
<ColumnDefinition Width='*' />
<ColumnDefinition Width='Auto' />
Text='This is my custom title bar' />
<!-- New Content Area -->
<TextBlock Text='Content goes here...' />
The main line of interest in this window is wiring up the OnDragMoveWindow handler to the MouseDown event of our titlebar grid.
The resulting Window looks like this:
You can’t move it yet, because we haven’t wired up the event handlers. You’ll also have a hard time closing it since we don’t have the close handler wired up.
Next we need to wire up the event handler to allow for moving the window around.
public partial class MainWindow : Window
private void WindowCloseButton_Click(object sender, RoutedEventArgs e)
private void OnDragMoveWindow(object sender, MouseButtonEventArgs e)
Note you can also use a command like ApplicationCommands.Close rather than working with an event handler for close. If you’re writing a real application, I suggest you take that route.
That’s all there is to it. If you want to have something else responsible for dragging the window, you simply wire up the DragMove to the MouseDown on that control.
One gotcha has to do with the space the OS normally allocates for the title bar. You’ll notice this if you try and dock the window in Windows 7. The API to address that takes a bit more work, so I’ll address it in a later post.
Published at DZone with permission of Pete Brown. See the original article here.
Opinions expressed by DZone contributors are their own.