如何在WPF中最大化后在MainWindow的中心显示子窗口? [英] How to display child window in the center of the MainWindow after Maximizing in WPF?

查看:133
本文介绍了如何在WPF中最大化后在MainWindow的中心显示子窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有MaxHeight =600MaxWidth =700的子窗口。当我最大化这个子窗口时,它显示在left = 0和top = 0 position.I希望它显示在中心MainWindow。



我试过以下方式



在Window_StateChanged事件中,我试图将其位置更改为left = 100和Top = 140.这些值已分配,但仍显示在left = 0和Top = 0 position。



我也试图改变它在Window_SizeChanged事件中的位置,但它也没有用。



然后我觉得布局可能没有刷新,所以我使用this.UpdateLayout()刷新它,但没有工作。



我的XAML文件供您参考。

< window x:class =MyClass.Window1xmlns:x =#未知>

xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x =http:// schemas.micr osoft.com/winfx/2006/xaml

xmlns:winForms =clr-namespace:System.Windows.Forms; assembly = System.Windows.Forms

xmlns :WFI =CLR-名称空间:System.Windows.Forms.Integration; assembly = WindowsFormsIntegration

Title =Window1Loaded =Window_LoadedFocusable =TrueShowInTaskbar =FalseResizeMode =CanResizeMaxHeight =600MaxWidth =700LocationChanged = Window_LocationChangedStateChanged =Window_StateChangedWindowStartupLocation =CenterOwner>

< grid>

< windowsformshost name =windowsFormsHost1>

$ b


解决方案

嗨Sudarshan,



你对这个问题的描述有点让人困惑(对我而言)。当我最大化这个窗口时是什么意思?



你孩子的xaml文件(我猜)也没多大帮助。



所以只需要一个简短的(一般)答案。



要将孩子置于其父Windows中心,您必须在显示孩子之前将孩子的所有者属性设置到父窗口。应该很容易并且总是有效。



但是如果我理解你的话,你想要在窗口显示之后居中窗口,当窗口最大化时(但不占用窗口)整个屏幕,因为你的MaxWidth和MaxHeight设置?如果是这样,我不知道一个常见的解决方案,你必须自己编程行为 - 也许这可能会有所帮助: http://stackoverflow.com/questions/4806088/how-can-i-find-the-position-of-a-maximized-window [ ^ ]



顺便说一句。我不想改变用户对常见操作系统操作的期望。 (如果我点击我希望它的任何窗口的最大化按钮 - 啊 - 最大化我的屏幕尺寸)。您的窗口将最大化到您的极限并居中于它的父级(如果您正常工作)。因此,我要么将其设置为没有最大化/最小化选项的对话框窗口,要么提供特殊菜单选项。所以也许你可以质疑这个要求...或者我想念你想要的东西......



更新:

一个简单的解决方案可以通过在 StateChanged 中重置 WindowState 来覆盖最大化行为:正常情况:



  if (WindowState == System.Windows.WindowState.Maximized)
{
WindowState = System.Windows.WindowState.Normal;
.Left = 500 ;
this .Top = 500 ;
// ... size logic ...
}











亲切问候Johannes


把它放在.XAML的Loaded事件中:



double screenWidth = System.Windows .SystemParameters.PrimaryScreenWidth;

double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;

double windowWidth = 500;

double windowHeight = 400;

this.Left =(screenWidth / 2) - (windowWidth / 2);

this.Top =(screenHeight / 2) - (windowHeight / 2);


Hey Sudarshan,



问题是 WindowStartupLocation 您孩子窗口中的财产。如果您将此属性值指定为 CenterOwner ,那么它将不会影响您的窗口位置,它始终显示在主窗口的中心。因此,当它的值 CenterOwner 时,你不能给出头寸。



http://msdn.microsoft.com /en-us/library/system.windows.window.windowstartuplocation(v=vs.110).aspx [ ^ ]



下面的一句话形式链接 -



设置 WindowStartupLocation 手册导致窗口根据其 Left Top 属性值进行定位。如果未指定 Left Top 属性,则其值由Windows确定。



我建议您在不指定 WindowStartupLocation 属性的情况下为您的子窗口提供职位。



希望这对你有帮助。


I have child window with MaxHeight="600" MaxWidth="700".When i maximize this child window,it gets display on left=0 and top=0 position.I want it to be display in the center of the MainWindow.

I have tried following ways

In Window_StateChanged event, i have tried to change its position as left=100 and Top=140.The values are assigned,but still it displays on left=0 and Top=0 position.

I also tried to change its position in Window_SizeChanged event,but it also did not work.

Then i felt the layout might not refresh,so i refreshed it using this.UpdateLayout(),but not worked.

My XAML File for your reference.
<window x:class="MyClass.Window1" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:winForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration; assembly=WindowsFormsIntegration"
Title="Window1" Loaded="Window_Loaded" Focusable="True" ShowInTaskbar="False" ResizeMode="CanResize" MaxHeight="600" MaxWidth="700" LocationChanged="Window_LocationChanged" StateChanged="Window_StateChanged" WindowStartupLocation="CenterOwner">
<grid>
<windowsformshost name="windowsFormsHost1">



解决方案

Hi Sudarshan,

Your description of the problem is a little confusing (to me). What do you mean with "when I maximize this window"?

Also the xaml file of your child (I guess) is not much help.

So just a short (General) answer first.

To center a child to it's parent Windows you have to set the Owner property of the child to the parent window before showing the child. This should be easy and always works.

But If I understand you right, you want to "center" the window after it was shown, when it's maximized (but doesn't occupy the whole screen because of your MaxWidth and MaxHeight settings? If so, I don't know a common solution for it you have to program the behaviour for yourself - maybe this could help: http://stackoverflow.com/questions/4806088/how-can-i-find-the-position-of-a-maximized-window[^]

Btw. I don't like to change user expectations on common OS operations. (If I click the maximize button of any window I expect it to - ahh - maximize to my screen size). Your window would maximize up to your limits and center to it's parent (if you get it working). So I'd either make it a "Dialog" Window without maximize/minimize option or provide a "special" menu option for that. So maybe you could question the requirement... Or maybe I missunderstood what you want..

Update:
A easy solution coul be to just "override" maximize behaviour by resetting WindowState in StateChanged Event to normal:

if (WindowState == System.Windows.WindowState.Maximized)
{
    WindowState = System.Windows.WindowState.Normal;
    this.Left = 500;
    this.Top = 500;
    // ... size logic ...
}






Kind regards Johannes


put this in the Loaded event of .XAML:

double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
double windowWidth = 500;
double windowHeight = 400;
this.Left = (screenWidth / 2) - (windowWidth / 2);
this.Top = (screenHeight / 2) - (windowHeight / 2);


Hey Sudarshan,

The problem is the WindowStartupLocation property in your child window. If you give this property value as CenterOwner then it will not give affect to your windows postions, it always display in center of your main window. So you can not give positions when its value is CenterOwner.

http://msdn.microsoft.com/en-us/library/system.windows.window.windowstartuplocation(v=vs.110).aspx[^]

The one sentence form below link -

Setting WindowStartupLocation to Manual causes a window to be positioned according to its Left and Top property values. If either the Left or Top properties aren't specified, their values are determined by Windows.

I suggest you to give positions to your child window without assigning WindowStartupLocation property.

Hope this will help you.


这篇关于如何在WPF中最大化后在MainWindow的中心显示子窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆