I have an auto-sized application user control in WPF (Height="Auto" Width="Auto"). When a bunch of elements inside it are resized, the windows stays the same rendered size. How do I force it to resize? I have tried this line Application.Current.MainWindow.SizeToContent = SizeToContent.WidthAndHeight;
in the function that resizes the components inside the window. The compiler gives me an error that there is no object associated with this call. I have tried this line MainWindowBorder.Height = Double.NaN;
to trick it to resize. No luck either.
Asked
Active
Viewed 857 times
0

Sean
- 336
- 1
- 3
- 15
-
Did you try window.height -= amount you resized your elements – Master117 May 11 '16 at 19:31
-
You could definitely keep track of how much everything is changing and manually do it how @Master117 mentioned. It can be a bit tedious to get the code in order, depending on how many controls you have and how they change, but it would definitely work. – Broots Waymb May 11 '16 at 19:33
-
All of the elements inside are re-sized automatically. I collapse or make visible elements inside a series of grids, so I can't manually change the size without tedious calculations. The window contains the collection of these auto-resized grids. – Sean May 11 '16 at 19:35
-
IIRC, you need to make the window non-user-resizable. – SLaks May 11 '16 at 19:38
-
If it's a grid i think you need to stick it into a panel or something similar for auto to work. Not 100% sure. – Master117 May 11 '16 at 19:38
-
This may be helpful for you. http://stackoverflow.com/questions/16922955/window-resize-handle-event – PamZy May 11 '16 at 19:43
-
Does Application.Current.MainWindow exist since you get an "no object associated with this call" error? – Master117 May 11 '16 at 20:21
-
No, it does not exist. I do not know why. The MainView is created. – Sean May 11 '16 at 20:23
-
http://stackoverflow.com/questions/2038879/refer-to-active-window-in-wpf Try to get the window of your UserControl? A Control can't exist on its own. – Master117 May 11 '16 at 20:28
-
That doesn't work either. It still comes up with no object associated. – Sean May 12 '16 at 14:44
-
This works better. Thanks! http://stackoverflow.com/questions/16236905/access-parent-window-from-user-control – Sean May 12 '16 at 14:55
3 Answers
1
Use SizeToContent in XAML
SizeToContent="WidthAndHeight"

Master117
- 660
- 6
- 21
-
You cannot put that in the XAML. I have tried putting that in my loading page code behind (MainView()) but it gives me the error: Does not contain defintion for SizeToContent,. Also, if you read my question above, I stated that I have tried this in the code behind. – Sean May 11 '16 at 20:13
-
-
http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/SizeToContentWidthAndHeight.htm – Master117 May 11 '16 at 20:16
-
1
If all the inside controls can be resized but the only one thing to resize is Window, How about this simple approach? Hope this helps..
int mycurrentscreenwidth = 1366;
int mycurrentwindowwidth = 1366;
var screen = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
var screenwidth = screen.Width;
if (Convert.ToInt32(screenwidth) < 1366)
{
double calculate_scalefactor= Convert.ToInt32(screenwidth) / (double)mycurrentscreenwidth;
double newwidth_tobescaled = mycurrentwindowwidth * calculate_scalefactor;
this.Width = newwidth_tobescaled;
}

Kay Lee
- 922
- 1
- 12
- 40
1
To obtain the window of your current user control, see:
Access parent window from User Control
Then use:
Window yourParentWindow = Window.GetWindow(userControl);
yourParentWindow.SizeToContent = SizeToContent.WidthAndHeight;