WPF中窗口标题栏上的双击事件 [英] double click event on window title bar in wpf

查看:846
本文介绍了WPF中窗口标题栏上的双击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在标题栏上实现双击事件以最大化和最小化窗口(以下xaml)

I want to implement double click event on Title bar for maximize and minimize the window(xaml below)

<Window

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xml:lang="en-US"

    xmlns:abc="clr-namespace:abc"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2006"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    xmlns:local="clr-namespace:abc"

    x:Class="abc.Window1"

    x:Name="Window"

    Title="Good People"

    MinHeight="100"

    MinWidth="200" AllowsTransparency="True" Background="Transparent" SizeChanged="Window_SizeChanged"

    Height="600" Width="900" WindowStyle="None" Loaded="Window_Loaded" PreviewMouseMove="ResetCursor"

    ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Icon="/abc;component/aexe.ico" PreviewKeyDown="Window_PreviewKeyDown" MouseLeftButtonDown="Window_MouseDown">


    <Border Name="windowborder1" BorderBrush="{DynamicResource MyDarkBlueSolidBrush}" Background="{DynamicResource TitleBorderBrush}" BorderThickness="1.5" CornerRadius="5">

        <Border Name="windowborder" BorderBrush="{StaticResource TitleBorderBrush}" Background="{StaticResource TitleBorderBrush}" BorderThickness="2" CornerRadius="5" >

            <Grid x:Name="LayoutRoot">

                    <Rectangle Fill="{StaticResource TitleBorderBrush}" Stroke="{x:Null}" VerticalAlignment="Top" Height="2.5" x:Name="top" PreviewMouseDown="Resize" MouseMove="DisplayResizeCursor" Margin="2.5,0,2.5,0"/>
                    <Rectangle Fill="{StaticResource TitleBorderBrush}" Stroke="{x:Null}" x:Name="bottom" Height="2.5" VerticalAlignment="Bottom" PreviewMouseDown="Resize" MouseMove="DisplayResizeCursor" Margin="2.5,0,2.5,0"/>
                    <Rectangle Fill="{StaticResource TitleBorderBrush}" Stroke="{x:Null}" HorizontalAlignment="Left" Margin="0,2.5,0,2.5" Width="2.5" x:Name="left" PreviewMouseDown="Resize" MouseMove="DisplayResizeCursor"/>
                    <Rectangle Fill="{StaticResource TitleBorderBrush}" Stroke="{x:Null}" Margin="0,2.5,0,2.5" Width="2.5" HorizontalAlignment="Right" x:Name="right" PreviewMouseDown="Resize" MouseMove="DisplayResizeCursor" />
                    <Rectangle Fill="{StaticResource TitleBorderBrush}" Stroke="{x:Null}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="2.5" Height="2.5" x:Name="bottomLeft" PreviewMouseDown="Resize" MouseMove="DisplayResizeCursor"/>
                    <Rectangle Fill="{StaticResource TitleBorderBrush}" Stroke="{x:Null}" VerticalAlignment="Bottom" Height="2.5" Width="2.5" HorizontalAlignment="Right" x:Name="bottomRight" PreviewMouseDown="Resize" MouseMove="DisplayResizeCursor"/>
                    <Rectangle Fill="{StaticResource TitleBorderBrush}" Stroke="{x:Null}" HorizontalAlignment="Right" Width="2.5" Height="2.5" VerticalAlignment="Top" x:Name="topRight" PreviewMouseDown="Resize" MouseMove="DisplayResizeCursor"/>
                    <Rectangle Fill="{StaticResource TitleBorderBrush}" Stroke="{x:Null}" HorizontalAlignment="Left" Width="2.5" VerticalAlignment="Top" Height="3" x:Name="topLeft" PreviewMouseLeftButtonDown="Resize"  MouseMove="DisplayResizeCursor"/>

                    <!-- Top PANEL -->

                    <DockPanel Name="Maindocpanal" Background="{DynamicResource TitleBorderBrush}" Margin="2.5,2.5,2.5,2.5">

                        <Grid Height="auto" Name="gridTitle" Width="auto" DockPanel.Dock="Top" MouseLeftButtonDown="mouseLeftButtonDown" Margin="0,0,0,0">


                            <!-- TITLE BAR -->

                            <Grid Name="maxMinCloseGrid"  Height="auto" Background="{DynamicResource TitleBorderBrush}"

                                ClipToBounds="False" Margin="0,0,0,0" IsHitTestVisible="True">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition  Width=".300*" />
                                    <ColumnDefinition Width=".700*" />
                                </Grid.ColumnDefinitions>

                                <Label Margin="105,5,0,1" Name="titleLabel" DockPanel.Dock="Top"

                                    Background="Transparent" ClipToBounds="False" Foreground="{DynamicResource MyDarkBlueSolidBrush}" FontFamily="Tahoma"

                                    Grid.Column="1" HorizontalAlignment="Left" Width="185" FontSize="14" FontStyle="Normal"

                                     >abc
                                </Label>
                            </Grid>
                            <!-- TITLE BAR -->
                    </DockPanel>
                    <!-- Top PANEL -->
            </Grid>
        </Border>
    </Border>

</Window>

推荐答案

嗨 您可以在XAML中使用以下事件:
Hi You can use the following event in the XAML:
PreviewMouseDoubleClick="Window_PreviewMouseDoubleClick"



您不必将其放在窗口"上.您可以在顶部创建自己的标题栏,然后将事件添加到该控件中.因为将其添加到窗口使它可以在整个窗口中工作.

然后在事件中添加以下代码:



You don''t have to put it on the "Window". You can create your own title bar in the top and add the event to that control instead. Because adding it to the Window make it work all over the window.

Then add the following code in the event:

private void Window_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if (this.WindowState == System.Windows.WindowState.Maximized)
        this.WindowState = System.Windows.WindowState.Minimized;
    else
        this.WindowState = System.Windows.WindowState.Maximized;
}



如果尚未最大化,则此方法将最大化;如果尚未最大化,则此方法将最小化.

希望它能对您有所帮助:-)



This method will maximize if not yet maximized, and minimize if maximized.

Hope it helped :-)


这篇关于WPF中窗口标题栏上的双击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆