Reputation: 984
I have a small problem with DragMove()
method. I want to call this when MouseLeftButtonDown
event on a Menu is handled (simply said, I want to drag the window via menu).
My XAML:
<Window x:Class="LoginForm.RidicWindow"
...namespaces...
Title="RidicWindow" Height="600" Width="800" WindowStyle="None" ResizeMode="NoResize" BorderBrush="#48067f" BorderThickness="2" Icon="img/EvidenceLogo.png">
some XAML
<Menu x:Name="Menu" Grid.Column="0" Grid.Row="0" MouseLeftButtonDown="Drag">
...rest of doc
And coresponding C# code:
private void Drag(object sender, MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
this.DragMove();
}
It doesn't work. I don't have any idea why. Menu is a descendant of UIElement
, so in my opinion everything appears to be OK.
I believe it's simple and I have a stupid mistake in code.
Upvotes: 0
Views: 3849
Reputation: 41
The Menu itself consumes the MouseLeftButtonDown event. So, no event handler is called. Only PreviewMouseLeftButtonDown will be raised. But calling DragMove() in this event prevents the normal menu function. Inside DragMove() a WM_LBUTTONUP message is send, after this method returns it seems like no button is pressed at all.
Solution can be: Put the Menu into a Grid with two colums, one Auto, one * (or omit the Width). Give the Grid a Background, otherwise it will not receive mouse events
<Grid MouseLeftButtonDown="handler">
<Grid.Background>
<SolidColorBrush Color="{DynamicResource {x:Static SystemColors.MenuBarColorKey}}" />
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Menu IsMainMenu="True">...</Menu>
</Grid>
On the left part the Menu consumes the click, on the right part the handler will be called.
Upvotes: 2