Before considering some complex alternatives, try adding an attribute to Dock Panel: Background="Red".
How to move a WPF application when DragMove() is not working?
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.
1 additional answer
Sort by: Most helpful
-
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:
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.