相对于主窗口调整内容和中心的新窗口大小

时间:2018-04-14 03:13:37

标签: c# wpf xaml

我有一个打开New Window

的按钮

我希望New Window符合内部的动态内容。

然后相对于Center定位Main Window。所以它遵循Main Window的位置。

不是WindowStartupLocation.CenterScreen

这就是我正在使用的。它不太正常。

XAML

New Window最初设置为900x500,但被SizeToContent覆盖。

<Window x:Class="MyProgram.NewWindow"
        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="New Window" 
        Width="900"
        Height="500">

C#

此按钮位于Main Window

我使用SizeToContent.Width

newWindow.Width不使用SizeToContent的宽度,而是检测XAML宽度900

这会导致New Window始终偏离中心。

我尝试将XAML宽度设置为Auto1,但仍然偏向不同的方向。

我尝试使用double width = Convert.ToInt32(SizeToContent.Width);,但它说宽度为1

我跑了newWindow.UpdateLayout()但是没有用。 https://stackoverflow.com/a/2149676/6806643

public static NewWindow newWindow;

private Boolean IsNewWindowOpened = false;

private void btnNeWindow_Click(object sender, RoutedEventArgs e)
{   
        // Check if Window is already open
        if (IsNewWindowOpened) return;

        newWindow = new NewWindow(this);

        // Only allow 1 Window instance
        newWindow.ContentRendered += delegate { IsNewWindowOpened = true; };
        newWindow.Closed += delegate { IsNewWindowOpened = false; };

        // Keep Window on Top
        newWindow.Owner = Window.GetWindow(this);

        // Fit Window to Content
        newWindow.SizeToContent = SizeToContent.Width;

        // Update Layout
        newWindow.UpdateLayout()

        // Detect which screen we're on
        var allScreens = System.Windows.Forms.Screen.AllScreens.ToList();
        var thisScreen = allScreens.SingleOrDefault(s => this.Left >= s.WorkingArea.Left && this.Left < s.WorkingArea.Right);
        if (thisScreen == null) thisScreen = allScreens.First();

        // Position Relative to MainWindow
        newWindow.Left = Math.Max((this.Left + (this.Width - newWindow.Width) / 2), thisScreen.WorkingArea.Left);
        newWindow.Top = Math.Max((this.Top + (this.Height - newWindow.Height) / 2), thisScreen.WorkingArea.Top);

        // Open Window
        newWindow.Show();
}

实施例

离开中心

off center

正确居中

center

1 个答案:

答案 0 :(得分:0)

这就是我所做的。如果您有任何改进,请告诉我。

主窗口

打开新窗口按钮

public static NewWindow newWindow;

private Boolean IsNewWindowOpened = false;

private void btnNeWindow_Click(object sender, RoutedEventArgs e)
{   
    if (IsPreviewWindowOpened) return;

    // Open Preview Window
    previewWindow = new Preview(this);

    // Only allow 1 Window instance
    previewWindow.ContentRendered += delegate { IsPreviewWindowOpened = true; };
    previewWindow.Closed += delegate { IsPreviewWindowOpened = false; };

    // Keep Window on Top
    previewWindow.Owner = Window.GetWindow(this);

    // Size to Content
    previewWindow.SizeToContent = SizeToContent.Width;

    // Open Window
    previewWindow.Show();
}

新窗口

<强> XAML

Window_Loaded

<Window x:Class="MyProgram.NewWindow"
        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="New Window" 
        Width="900"
        Height="500"

        Loaded="Window_Loaded">

<强> C#

尺寸窗口以适应动态内容。
位置中心,相对于主窗口。

如果关闭,也会在屏幕上轻推窗口。

private void Window_Loaded(object sender, EventArgs e)
{
    // Detect which screen we're on
    var allScreens = System.Windows.Forms.Screen.AllScreens.ToList();
    var thisScreen = allScreens.SingleOrDefault(s => this.Left >= s.WorkingArea.Left && this.Left < s.WorkingArea.Right);
    if (thisScreen == null) thisScreen = allScreens.First();

    // Position Relative to MainWindow
    this.Left = Math.Max((mainwindow.Left + (mainwindow.Width - this.Width) / 2), thisScreen.WorkingArea.Left);
    this.Top = Math.Max((mainwindow.Top + (mainwindow.Height - this.Height) / 2), thisScreen.WorkingArea.Top);
}