How to move a WPF application when DragMove() is not working?

MERUN KUMAR MAITY 511 Reputation points
2022-01-04T20:02:15.327+00:00

I have a WPF application where I can not move my application according to my mouse movement sometimes it's freeze or shut down. I use fluid layout that means I use Grid.row definition to distribute my Grid space.

 <Grid.RowDefinitions>
            <RowDefinition Height="Auto" MinHeight="50"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto" MaxHeight="31"/>
        </Grid.RowDefinitions>

In the first Row I use a Dockpanel. Actually I want to use the first row as the title bar and if Someone click the mouse on it and drag my application then it must be move but unfortunately it can not happen.

Here is my Dockpanel code :

 <DockPanel x:Name="yp" Grid.Row="0" LastChildFill="False" HorizontalAlignment="Stretch" PreviewMouseLeftButtonDown="yp_PreviewMouseLeftButtonDown" >

        </DockPanel>

I try using DragMove() my in Mousedown event but nothing works,
Here is the code :

private void yp_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

The main reason is my UI somehow get locked and unable to call the Win32 API, I think. If any win32 solution or anything is present then please help.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,686 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,363 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.9K Reputation points
    2022-01-04T21:05:34.67+00:00

    Before considering some complex alternatives, try adding an attribute to Dock Panel: Background="Red".


1 additional answer

Sort by: Most helpful
  1. Hui Liu-MSFT 41,146 Reputation points Microsoft Vendor
    2022-01-05T03:17:46.787+00:00

    You could try to refer to the following example.
    MainWindow.xaml:

    <Grid >  
            <Grid.RowDefinitions>  
                <RowDefinition Height="Auto" MinHeight="50"/>  
                <RowDefinition Height="*"/>  
                <RowDefinition Height="Auto" MaxHeight="31"/>  
            </Grid.RowDefinitions>  
            <DockPanel x:Name="yp" Grid.Row="0" Background="LightSeaGreen"  Loaded="header_Loaded"  LastChildFill="False"  Width="800" HorizontalAlignment="Left" >  
                <TextBlock x:Name="header" Background="AliceBlue" Height="50" Text="header" Width="700" />  
            </DockPanel>  
        </Grid>  
    

    MainWindow.xaml.cs:

     public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
        }  
        private void header_Loaded(object sender, RoutedEventArgs e)  
        {  
          InitHeader(sender as DockPanel);  
        }  
        private void InitHeader(DockPanel header)  
        {   
      
          header.MouseLeftButtonDown += (s, e) =>  
          {  
            DragMove();  
          };  
        }  
      }  
    

    The result:
    162393-1.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.