Click here to Skip to main content
15,904,415 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to implement double click event on Title bar for maximize and minimize the window(xaml below)

XML
<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>
Posted

1 solution

Hi You can use the following event in the XAML:
HTML
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:
C#
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 :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900