如何将单击事件添加到自定义WPF WindowTitle
我重新设计了WPF窗口的样式,一切都很好。但是,当有人单击标题栏时,我现在需要显示一个弹出菜单。我不明白我如何才能做到这一点,因为在样式中的一个边框中,通过鼠标向下点击标题,这是我应用于多个窗口的通用样式 窗口样式如下所示:如何将单击事件添加到自定义WPF WindowTitle,wpf,events,event-handling,window,Wpf,Events,Event Handling,Window,我重新设计了WPF窗口的样式,一切都很好。但是,当有人单击标题栏时,我现在需要显示一个弹出菜单。我不明白我如何才能做到这一点,因为在样式中的一个边框中,通过鼠标向下点击标题,这是我应用于多个窗口的通用样式 窗口样式如下所示: <Style x:Key="MYWindow" TargetType="{x:Type Window}"> <Setter Property="ResizeMode" Value="NoResize" /> <Setter Pr
<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"/>
我重复使用以下样式:
partial class MYStyles
{
public void HandleTitleBarMouseDown(object sender, MouseButtonEventArgs e)
{
Window window = GetWindow(sender);
window.DragMove();
}
}
<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="">
我需要在Window1类中以某种方式引发一个事件,但不知道如何实现此目的?您可以使用
MouseMove
事件进行拖动:
private void HandleTitleBar_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Window window = GetWindow(sender);
window.DragMove();
}
}
然后,您可以使用
MouseDown
事件来显示自定义的弹出窗口
,以及所需的内容。您可以使用全局事件挂钩
在app.xaml.cs或启动应用程序时调用的任何类中放置此行:
EventManager.RegisterClassHandler(typeof(Window), Window.PreviewMouseUpEvent, new MouseButtonEventHandler(OnWindowPreviewMouseUp));
并声明OnWindowPreviewMouseUp事件处理程序:
void OnWindowPreviewMouseUp(object sender, MouseButtonEventArgs e)
{
//do your implementation
}