Click here to Skip to main content
15,877,675 members
Please Sign up or sign in to vote.
3.30/5 (4 votes)
See more:
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">



Posted

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:

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





Kind regards Johannes
 
Share this answer
 
v2
Comments
Sudarshan Gaikwad 10-Jul-14 3:39am    
Hi Johannes,

For your first confusion, "when I maximize this window ?".
=> This window means my child window(I have mentioned in my question also).

For your second confusion,"After maximizing child window,it should not occupy whole screen"
=> Correct(Its requirement,as i am showing picture in that window,it should not cover whole area after maximize).

The link provided did not help me.As i am unable re-position child window after maximize.Top=140 and left=100 are correct,but still child displayed on top=0 and left=0 position.
johannesnestler 10-Jul-14 4:37am    
Ah, so the solution could be just to "override" maximize to do someting else - see my updated solution
Sudarshan Gaikwad 10-Jul-14 4:56am    
If i do this the state of the window would be normal and i could not be able to "restore down" the window.
johannesnestler 10-Jul-14 10:54am    
ah I see, this may be a problem. But may I ask again - what is wrong with a "enlarge" button (or something similar) to watch the picture as big as possible, like many Websites do. (and won't mess with the "normal" maximize behaviour, make it like a toolwindow without maximize, minimize buttons.) It seems there is no easy solution to control "maximized" window location in non MDI-Child windows. Though, you could extend my approach by overriding the System menu - but that is very difficult, I won't do it.
johannesnestler 10-Jul-14 10:55am    
btw, didn't get the notice of your comment (again), seems somethings broken with this Feature hers on CP
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);
 
Share this answer
 
v2
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900