Wpf Always on Top

How do I get WPF Window to stay on top of another?

Suppose you have a MainWindow:

<Window x:Class="SO40189046.MainWindow"
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"
xmlns:local="clr-namespace:SO40189046"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Name="TimeText" />
</Grid>
</Window>

with code behind:

using System;
using System.Threading;
using System.Windows;

namespace SO40189046
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;

// Background thread initializing the MainWindow
ThreadPool.QueueUserWorkItem((state) =>
{
for (int i = 0; i < 10; i++)
{
Dispatcher.Invoke(() =>
{
TimeText.Text = DateTime.Now.ToString();
});
Thread.Sleep(1000);
}
});

}

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{

LoginWindow login = new LoginWindow();
login.Owner = App.Current.MainWindow;
login.WindowStartupLocation = WindowStartupLocation.CenterOwner;
if (!login.ShowDialog().GetValueOrDefault())
{
Close();
}
}
}
}

Then you can initialize your MainWindow while showing the login dialog.

AND you load the MainWindow as normal via StartUpUri in App.xaml

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

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.

WPF always on top only for parent

Set the Owner property of your child window, pointing to the opener window. This way it will always be on top of its parent.

SomeWindow childWindow = new SomeWindow();
childWindow.Owner = this;
childWindow.Show();


Related Topics



Leave a reply



Submit