Determine size of SizeToContent WPF Window before its rendered - c#

I have a window in my WPF application that is displayed on occasion. When it is shown it is faded in with an annimation, and when closed it is faded out. Nothing fancy, just a storyboard that modifies the opacity. Actually the window is never really closed, the opacity is just faded out to 0 where it remains until its to be displayed again.
This window is an informative window and doesn't always show the same content. It is sized to content (Width and Height) and works well in that regard. The user chooses the basic area of the screen for it to be displayed (TopLeft, TopRight, Center, BottomLeft, BottomRight).
Before the window is faded in the content is updated. Because the window is sized to content it increases or descreases in size. The width and height can change.
When positioning the window, lets say, in the bottom right corner, I simply take the WorkingArea of the screen (width and height) and then minus the width/height of the window to get the Top and Left position that I need.
The logic works, but the trouble I am having is the Window's Height and Width is not returning the size it is after the content was updated, but is returning the size it was the last time it was displayed. I am assuming this is because it hasn't yet been rendered with the new content.
This causes me grief becuase if the Window is larger than it was the last time it obviously extends off the screen.
I tried positioning the window in the OnContentRendered event, but this only fires once when the Window is created, not after the content has been updated, when the opacity is set to 0.
Does anyone have any idea how I might get an accurate width and height of this window before it is faded in?
Any help would be appreciated!!

I found the answer to my own question. After updating the content a simple call to the window's UpdateLayout() method forces an update. Calls to the width and height then return accurate values.

Related

Popup dimensions on screen

I thought I understood the WPF content scaling system but ran into an issue that's mysterious to me. I have a very simple popup that is supposed to be used as a loupe that opens on top of an image when the user clicks the image. The XAML is this:
<Popup Name="LoupePopup" AllowsTransparency="True" IsOpen="False" StaysOpen="True">
<Border BorderThickness="2" BorderBrush="Azure" />
</Popup>
In code behind, I then tie the popup's placement to an image that is already showing when the popup is opened. I set the loupe size to half the size of the image with this code:
LoupePopup.PlacementTarget = FullImg;
LoupePopup.Placement = PlacementMode.Relative;
LoupePopup.Width = FullImg.ActualWidth / 2;
LoupePopup.Height = FullImg.ActualHeight / 2;
Further code then moves the loupe along with MouseMove, but that does not really relate to the issue here: I'd expect the loupe to be exactly half the width/height of the image, but it is not - it is quite a bit larger (by about 18%). I verified the actual image size and the actual size of the window on which it is displayed, as well as the mouse coordinates. Those dimensions/point coordinates all made perfect sense, but why would the popup not use the same width/height scaling as the other elements here?
For clarification: The image covers 80% or so of the screen, so this is not just an issue of a few pixels. The window is a child window created in code, and the popup is defined in the XAML of the main window, but both windows are set to full screen size.
Appreciate your thoughts!
Maybe it is so because border thickness is not taking into account when calculating LoupePopup size
or default margin and padding of popup or image
I'm not sure, but I think you should have a look at the Stretch property of the image. If you have a width and a height defined on the image the ActualWidth/-Height are influenced by the Stretch property. Setting it to Fill makes the ActualWidth/-Height equal to the defined Width and Height.
After trying more options I had to realize that the child-versus main window aspect caused the issue. The image was showing in a child window; the popup was defined in the main window. After moving the popup XAML to the child window, the popup dimensions were correct.
Not sure I understand why (both windows have the full screen dimensions and should use the same scaling, but at least this solves my issue. Thanks for trying to help!

MahApps ActualWidth when maximized too high

I'm having some problem with getting window size of MahApps window.
It's working perfectly when not maximized, but if I maximize it, it's too high;
I have code behind like this:
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
UpRect1.Width = ActualWidth;
}
And in ActualWidth property is proper value when windows IS NOT maximized, but if I maximize window i get higher value than my resolution is (I have 1366 screen width, it returns 1382).
Is it some kind of bug, or it's standard Windows crazy behaviour?
P.S.
Same stuff with height
P.S 2
I have also checked with Width property, but it's not updated on resize
That's the normal behaviour of Windows and has nothing to do with MahApps.Metro. See Raymond Chen's "Why are the dimensions of a maximized window larger than the monitor?":
The extra eight pixels that hang off the screen are the window borders. When you maximize a window, the window manager arranges things so that the client area of the window fills the width of your work area, and it plus the caption bar fills the height of the work area. After all, you want to see as much of your document as possible; there's no need to show you the window borders that aren't doing you any good. (It leaves the caption bar on the screen for obvious reasons.)

Get Width and Height of Windows Form C# After user clicks on Restore

I want to get the exact width and height of the windows form. Initially I set this 1024*768 but when user clicks on restore button its width may vary according to User machine screen resolution. So how can i get the exact height and width of the form when user maximized the window.
Use Resize event. This event occurs when user resizes the form in any way, whether through Maximize/Restore buttons, or manually dragging the edges of the form. Therein you can use Form.ClientRectangle or Form.Size properties to get the exact width and height of your form after it is resized.

Is there a way to leave the resolution of a C# Windows Form Application fixed?

I want to fix the resolution of my main form, I can set the number of pixels using
this.Height = 480;
this.Width = 640;
But I want some way to make it constant, so that even if the frame is enlarged, the number of pixels will not change, but their size.
By setting the option AutoSizeMode to GrowAndShrink I can prevent re-sizing of the form by dragging the edges of the form, but when the maximize button is pressed, the form becomes big and adds new pixels. I want to allow the re-sizing of the form and putting it in maximized or fullscreen mode without changing the number of pixels in the form, or in other words, enlarging the size if the current pixels, instead of adding new pixels.
Is there a way too do it?
Pixels are fixed units. You probably would like WPF since its a vector based system.

Is there a way to determine the width and height of a WPF Window before showing it?

I am trying to center a WPF Window on the current position of the mouse cursor. Getting the mouse coordinates is no problem, but I need to determine the width and height of the window in order to calculate the offset for setting the 'Left' and 'Top' properties.
Is there a way to determine the width and height of a window before showing it (calling the 'Show' method)?
I have 'SizeToContent' set to true on my window so that the content determines the actual window size. I have already tried calling 'Measure' and was expecting this to set 'DesiredSize' but it didn't.
Why not call Show() first and then center it on the current position of the mouse cursor?

Categories

Resources