Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WPF, can I have a borderless window that has regular minimize, maximise and close buttons?

Tags:

wpf

xaml

If you look at the Chrome browser when maximized, it has its tab headers right at the top of the window. Can I do something similar?

like image 597
James L Avatar asked Dec 18 '12 10:12

James L


People also ask

How remove maximize and minimize button in WPF?

By setting the ResizeMode property to CanMinimize only the Minimize button will be visible. The maximize button will be hidden and the resizing will be disabled.

How do I make WPF window open full screen?

Just set the WindowState to Maximized , and the WindowStyle to None . Also setting the Window as topmost will make sure no other Window shows up over your window.

How do I close a window in WPF?

Pressing ALT + F4 . Pressing the Close button. Pressing ESC when a button has the IsCancel property set to true on a modal window.


1 Answers

Absolutely, but you're going to have to remake those buttons yourself (it's not hard, don't worry).

In your MainWindow.xaml:

<Window ...
        Title="" Height="Auto" Width="Auto" Icon="../Resources/MyIcon.ico" 
        ResizeMode="NoResize" WindowStartupLocation="CenterScreen" 
        WindowStyle="None" AllowsTransparency="True" Background="Transparent"
        ...>
    <Canvas>
       <Button /> <!-- Close -->
       <Button /> <!-- Minimize -->
       <Button /> <!-- Maximize -->
       <TabControl>
           ...
       </TabControl>
    </Canvas>
</Window>

Then you just have to place the Buttons and the TabControl as wished on the Canvas, and customize the look and feel.

EDIT: The built in commands for closing/maximizing/minimizing in .NET 4.5 are SystemCommands.CloseWindowCommand/ SystemCommands.MaximizeWindowCommand/SystemCommands.MinimizeWindowCommand

So if you're using .NET 4.5, you can do:

<Window ...
        Title="" Height="Auto" Width="Auto" Icon="../Resources/MyIcon.ico" 
        ResizeMode="NoResize" WindowStartupLocation="CenterScreen" 
        WindowStyle="None" AllowsTransparency="True" Background="Transparent"
        ...>
    <Window.CommandBindings>
        <CommandBinding Command="{x:Static SystemCommands.CloseWindowCommand}" CanExecute="CommandBinding_CanExecute_1" Executed="CommandBinding_Executed_1" />
        <CommandBinding Command="{x:Static SystemCommands.MaximizeWindowCommand}" CanExecute="CommandBinding_CanExecute_1" Executed="CommandBinding_Executed_2" />
        <CommandBinding Command="{x:Static SystemCommands.MinimizeWindowCommand}" CanExecute="CommandBinding_CanExecute_1" Executed="CommandBinding_Executed_3" />
    </Window.CommandBindings>
    <Canvas>
       <Button Command="{x:Static SystemCommands.CloseWindowCommand}" Content="Close" />
       <Button Command="{x:Static SystemCommands.MaximizeWindowCommand}" Content="Maximize" />
       <Button Command="{x:Static SystemCommands.MinimizeWindowCommand}" Content="Minimize" />
       <TabControl>
           ...
       </TabControl>
    </Canvas>
</Window>

And in your C# code-behind:

    private void CommandBinding_CanExecute_1(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }

    private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e)
    {
        SystemCommands.CloseWindow(this);
    }

    private void CommandBinding_Executed_2(object sender, ExecutedRoutedEventArgs e)
    {
        SystemCommands.MaximizeWindow(this);
    }

    private void CommandBinding_Executed_3(object sender, ExecutedRoutedEventArgs e)
    {
        SystemCommands.MinimizeWindow(this);
    }

This will make close/maximize/minimize works exactly like with a regular window.
Of course, you may want to use System.Windows.Interactivity to move the C# into a ViewModel.

like image 130
Louis Kottmann Avatar answered Oct 03 '22 23:10

Louis Kottmann