Search code examples
c#wpfwindowsdockappbar

How to implement a window that sits on top and reduces the working space area, using C# and WPF?


I have a WPF window declared like this

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="MyWindow"
    Height="800" Width="200"
    BorderThickness="0"
    WindowStyle="None"
    AllowsTransparency="False"
    ResizeMode="CanResizeWithGrip"
    Topmost="True"
    Background="#fff59d"
    ShowInTaskbar="False">

    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="0" ResizeBorderThickness="5" />
    </WindowChrome.WindowChrome>
</Window>

In this window there are a bunch of aligned buttons, making the window a nice tool bar that sits on top of the other windows.

Now I'd like to snap it to a screen edge (bottom, left, top, right) so that the working area of the screen is reduced by the window's area. Just like what happens with the Windows taskbar: the area covered by the taskbar is not used when other windows are maximized and the taskbar is always on top.

Any help is much appreciated !

EDIT

I'm adding an image to better explain my question:

enter image description here

I'm interested in position my WPF window on an edge so that the area of the window is forbidden to other windows.


Solution

  • First use Top="0" Left="0" to snap your "nice tool bar window" to the top and left edges of the screen. Second use the event Window_Loaded to set the window height equal to the screen height minus the taskbar height so that it will not be on top of it.

    Side note Title doesn't make sens in your case

    XAML:

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Width="200"
        BorderThickness="0"
        WindowStyle="None"
        AllowsTransparency="False"
        ResizeMode="CanResizeWithGrip"
        Topmost="True"
        Top="0"
        Left="0"
        Loaded="Window_Loaded"
        Background="#fff59d"
        ShowInTaskbar="False">
    
        <WindowChrome.WindowChrome>
            <WindowChrome CaptionHeight="0" ResizeBorderThickness="5"/>
        </WindowChrome.WindowChrome>
    </Window>
    

    Code-Behind:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      double TaskBarHeight = SystemParameters.PrimaryScreenHeight - SystemParameters.WorkArea.Height;
      Height = SystemParameters.PrimaryScreenHeight - TaskBarHeight;
    }
    

    Edit

    As @Bradley_Uffner explained in his comment you need an AppBar, you may want to take a look at Github.WpfAppBar or better check your options in this answer C# Window Docking.