Drag and Drop Window in WPF
XAML WPF Developer
36
Reputation points
Hi,
How to drag and drop wpf window, When selected title bar in top of window.
Thanks
Developer technologies | Windows Presentation Foundation
2,854 questions
Developer technologies | XAML
859 questions
2 answers
Sort by: Most helpful
-
-
Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
2021-11-30T07:44:45.233+00:00 For dragging and moving the window through TitleBar, you could try to refer to the following code
MainWindow.xaml:<Window x:Class="DragAndDrop.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:DragAndDrop" mc:Ignorable="d" WindowStyle="None" WindowState="Normal" Title="MainWindow" Height="450" Width="800"> <Window.Template> <ControlTemplate TargetType="Window"> <Border BorderBrush="red" BorderThickness="3"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="30"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Border BorderBrush="Purple" BorderThickness="2"> <TextBlock x:Name="header" Background="AliceBlue" Loaded="header_Loaded">header....</TextBlock> </Border> <ContentPresenter Grid.Row="1" /> </Grid> </Border> </ControlTemplate> </Window.Template> <Grid Background="LightGoldenrodYellow"> </Grid> </Window>
MainWindow.xaml.cs:
using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace DragAndDrop { public partial class MainWindow : Window { private bool mouseDown; public MainWindow() { InitializeComponent(); } private void header_Loaded(object sender, RoutedEventArgs e) { InitHeader(sender as TextBlock); } private void InitHeader(TextBlock header) { header.MouseUp+=new MouseButtonEventHandler(OnMouseUp); header.MouseLeftButtonDown += new MouseButtonEventHandler(MouseLeftButtonDown); header.MouseMove += new MouseEventHandler(OnMouseMove); } private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.ClickCount == 2) { if ((ResizeMode == ResizeMode.CanResize) || (ResizeMode == ResizeMode.CanResizeWithGrip)) { SwitchState(); } } else { if (WindowState == WindowState.Maximized) { mouseDown =true; } DragMove(); } } private void OnMouseUp(object sender, MouseButtonEventArgs e) { mouseDown=false; } private void OnMouseMove(object sender, MouseEventArgs e) { if (mouseDown) { var mouseX = e.GetPosition(this).X; var width = RestoreBounds.Width; var x = mouseX - width / 2; if (x < 0) { x = 0; } else if (x + width > SystemParameters.PrimaryScreenWidth) { x = SystemParameters.PrimaryScreenWidth - width; } WindowState = WindowState.Normal; Left = x; Top = 0; DragMove(); } } private void SwitchState() { switch (WindowState) { case WindowState.Normal: { WindowState = WindowState.Maximized; break; } case WindowState.Maximized: { WindowState = WindowState.Normal; break; } } } } }
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.[5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html