Windows can be moved on the screen by dragging their title bar; they can be resized by dragging their borders. But what if our window needs no title bar / borders (or they are custom drawn)? (aka WindowStyle="None"
).
Introducing MoveResizeBehavior, a WPF custom behavior that can be added to any FrameworkElement
will provide move and resize functionality for its parent window.
<Border DockPanel.Dock="Top" Height="11" Margin="7" b:MoveResizeBehaviour.ResizeWindow="Top"/> <Border DockPanel.Dock="Bottom" Height="11" Margin="7" b:MoveResizeBehaviour.ResizeWindow="Bottom"/> <Border DockPanel.Dock="Right" Width="11" Margin="7,0" b:MoveResizeBehaviour.ResizeWindow="Right"/> <Border DockPanel.Dock="Left" Width="11" Margin="7,0" b:MoveResizeBehaviour.ResizeWindow="Left"/> <Border Height="43" VerticalAlignment="Top" b:MoveResizeBehaviour.MoveWindow="True"/>
where
xmlns:b="clr-namespace:AndreiPana.WPF.Utilities"
ResizeWindow
can take one of the following values: None
, Left
, TopLeft
, Top
, TopRight
, Right
, BottomRight
, Bottom
, BottomLeft
, while the MoveWindow can be either True
or False
.
Internally, the behaviors attach to the MouseDown
event, so the FrameworkElement
s on which they will be set better not process themselves MouseDown
, otherwise it might interfere with the behaviors.
A simple and small test project showing the functionality is available on Github but, for convenience, here’s the MoveResizeBehavior.cs file that you can copy/paste to any WPF project and used it as it is: