I have restyled a WPF window, everything works really well. However, I now need to display a pop-up menu when someone clicks on the title bar. I don’t don’t understand how I can do this as the title down click is handled by a mouse down in one of borders in the style, and this is generic style that I am applying to several windows?
The window style looks like this:
<Style x:Key="MYWindow" TargetType="{x:Type Window}">
<Setter Property="ResizeMode" Value="NoResize" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="Background" Value="{StaticResource BGBrush}" />
<Setter Property="Foreground" Value="{StaticResource FGBrush}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="26"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="26"/>
<ColumnDefinition Width="26"/>
<ColumnDefinition Width="26"/>
</Grid.ColumnDefinitions>
<Border Background="{TemplateBinding Background}" BorderThickness="1" BorderBrush="{x:Null}" CornerRadius="5,5,5,5" BitmapEffect="{StaticResource DropShadow}" Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="4"/>
<ContentPresenter Margin="2" ClipToBounds="True" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4"/>
<Border Background="{StaticResource GreyGradientBrush}" BorderThickness="1" BorderBrush="{x:Null}" CornerRadius="5,5,0,0" BitmapEffect="{StaticResource WindowTitleShadow}" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" MouseDown="HandleTitleBarMouseDown"/>
Currently HandleTitleBarMouseDown handles the dragging of the window as follows:
partial class MYStyles
{
public void HandleTitleBarMouseDown(object sender, MouseButtonEventArgs e)
{
Window window = GetWindow(sender);
window.DragMove();
}
}
I re-use the style as follows:
<Window x:Class="TestWPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:TestWPF"
Style="{DynamicResource MYWindow}" Title="Test MY Window" Height="599" Width="891" SnapsToDevicePixels="False" WindowStyle="None" Padding="0" MouseDown="">
I need to somehow have an event raised in the Window1 class but have no idea how to achieve this?
You can use global event hooking.
Put this line in your app.xaml.cs or whatever class is called when you start the application:
And declare the OnWindowPreviewMouseUp event handler: