How to Center Your Main Window in Wpf

How do you center your main window in WPF?

private void CenterWindowOnScreen()
{
double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
double windowWidth = this.Width;
double windowHeight = this.Height;
this.Left = (screenWidth / 2) - (windowWidth / 2);
this.Top = (screenHeight / 2) - (windowHeight / 2);
}

You can use this method to set the window position to the center of your screen.

How to center a WPF app on screen?

Put this in your window constructor

WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

.NET FrameworkSupported in: 4, 3.5,
3.0

.NET Framework Client ProfileSupported
in: 4, 3.5 SP1

WPF maximize main window with center for application

Set the WindowState property instead of Width and Height:

mainWindow.WindowState = WindowState.Maximized;

WPF C# Send Window to Screen and align

First, once you close the window, it's disposed and you can't show it again. Use Show() and Hide() instead.

When you set WindowStartupLocation to CenterScreen ther is a problem: after the window is shown any programmatical changes to its position will be ignored. So you can only do it once this way.

To do it manually, you need to set WindowStartupLocation to Manual and then set your window's Top and Left properties.

The next problem you'll have to consider is the fact that there may be multiple monitors. If you only need to center the window on the monitor set as primary in the system, then:

w.Left = (SystemParameters.WorkArea.Width - w.ActualWidth) / 2 + SystemParameters.WorkArea.Left;
w.Top = (SystemParameters.WorkArea.Height - w.ActualHeight) / 2 + SystemParameters.WorkArea.Top

is enough. If you want to center the window on the current monitor, then you'll need to do something like this:

(method from here):

public static void PostitionWindowOnScreen(Window window, double horizontalShift = 0, double verticalShift = 0)
{
Screen screen = Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(window).Handle);
window.Left = screen.Bounds.X + ((screen.Bounds.Width - window.ActualWidth) / 2) + horizontalShift;
window.Top = screen.Bounds.Y + ((screen.Bounds.Height - window.ActualHeight) / 2) + verticalShift;
}

To call it:

TestWindow w;

//....

w = new TestWindow();
w.WindowStartupLocation = WindowStartupLocation.Manual;
w.Closed += w_Closed;

//....

private void w_Closed(object sender, EventArgs e)
{
// to make sure there is no disposed window to access when user closes it manually
w = null;
// or:
// w = new TestWindow();
}

private void Button_ShowWindow_Click(object sender, RoutedEventArgs e)
{
if (w == null) return;
PostitionWindowOnScreen(w, 0, 0);
w.Show();
}

private void Button_HideWindow_Click(object sender, RoutedEventArgs e)
{
if (w == null) return;
w.Hide();
}

How to make same center for main window and its navigated pages in wpf navigation

NavigationWindow nav = new NavigationWindow(); 
nav.Content = new Page1();

nav.WindowStartupLocation = WindowStartupLocation.Manual;

var screenW =
System.Windows.SystemParameters.PrimaryScreenWidth;
var windowW = nav.Width;
var xAxis = 0;

if ((screenW - windowW)>0)
{
xAxis = Convert.ToInt32((screenW - windowW) / 2);
}

nav.Left = xAxis; //Left position to set window at centre else to the left

nav.Top = (System.Windows.SystemParameters.PrimaryScreenHeight /8);

//Fixed top positio

nav.ShowDialog(); 

WPF - XAML Page Center to Window

Since you are using a grid, you can insert a stackpanel and center them out, like this:

<Grid VerticalAlignment="Center">
<StackPanel HorizontalAlignment="Center">
<Frame Source="\Views\LoginView.xaml" NavigationUIVisibility="Hidden" Name="MainFrame"></Frame>
</StackPanel>
</Grid>

WPF : How to set a Dialog position to show at the center of the application?

You can try to get a hold of the MainWindow in the Loaded event like this

private void Window_Loaded(object sender, RoutedEventArgs e)
{
Application curApp = Application.Current;
Window mainWindow = curApp.MainWindow;
this.Left = mainWindow.Left + (mainWindow.Width - this.ActualWidth) / 2;
this.Top = mainWindow.Top + (mainWindow.Height - this.ActualHeight) / 2;
}

Centering a WPF control

Use a Grid:

  <Grid>  
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Replace with your UserControl -->
<Button Content="Foo" Grid.Column="1" Grid.Row="1"/>
</Grid>

You can dock it inside your DockPanel (if you must have a DockPanel there) to stretch. And, of course, while the above is all markup, you can just as easily create such a grid from code.



Related Topics



Leave a reply



Submit