Set WPF/XAML window client area size - wpf

I have a pixel perfect control layout for a form I want to create. I know the exact width, height and location of each control.
But I am finding it difficult to set it perfectly inside a Window element.
The Width and Height property of the Window is including the borders. How can I set the inner width and height of the window? The Client width and height.
Say, my form is 300px wide. If I set the width and height of the Window to 300, then the borders take up anywhere from 5 - 15px and only 285 or so pixels are available for the client area where you can add controls. I want to set this client area's width to 300px.
I tried having a container control like a Grid or StackPanel and set its width to 300px and setting the window's widthto auto. But the window was way too wide.
Hope I am making sense.
How can I do it?

Your approach of setting an inner panel width and letting the window size itself accordingly is correct. What you probably might have missed is the property SizeToContent of the window. Set SizeToContent="WidthAndHeight" and it will work.

Related

Animation for SizeToContent resizing

In WPF window, I have specified SizeToContent="WidthAndHeight".
I have a number of panels stacked up, of which I change the visibility (to Visible or Collapsed) as required. My window resizes itself according to the panel that I have set visible. No issues here.
My problem is with the resizing, that the window resizes with a jerk to the final size.
I want to animate this such that the resizing is smooth. And i want to do this in my XAML file.
Is there any solution for this???
P.S. - I have not set any Width or Height property in the window and I want to restrict myself from doing that.

WP7 max circle size for orientation

In my WP7 app, I have a user control, with a grid, and an ellipse in the layout root:
<Grid x:Name="LayoutRoot">
<Grid x:Name="grdCircle">
<Ellipse x:Name="elCircle" Stroke="#FFB91515" Margin="5"/>
</Grid>
</Grid>
I drop this on my main page in the WP7 app, and it looks fine in landscape mode, but when I switch to portrait the width expands and the height contracts, so it is no longer a circle. What I want is for the circle to be the max size it can be regardless of the orientation and still stay a circle.
I've tried putting SizeChanged event on LayoutRoot, and setting the grdCircle width/height to whatever was smaller - the LayoutRoot actual width or the LayoutRoot actual height, but as soon as I do that, changing the orientation doesn't fire the SizeChanged event of LayoutRoot anymore because LayoutRoot also becomes smaller. How can I ensure that my ellipse is always a circle and grows/shrinks based on the orientation?
Edit:
By default, the LayoutRoot grid should have horizontal and vertical alignment set to stretch with margins of 0, so shouldn't the LayoutRoot grid always grow to the size of its container?
Maybe you should take adventage of OrientationChanged event of a Page?
Updated
I think that your control is filling all space that is available. If you change orientation then the amount of space is changing - as a result your control is not a square any more. That fact implicates that the ellipse changes its shape from circle to ellipse, because your ellipse is also trying to fill in all available space. To avoid this you can set Stretch property of an ellipse to Uniform. This should resolve your problem.

UserControl Width is Nan while it's ActualWidth set to the Column it placed on

I have a UserControl, I do not set it's Width / Height, the UserControl has 2 TextBox inside a StackPanel.
when I place it in a Column where it's Width="Auto", the control stretches to fill the entire column, while the 2 TextBox maintain their Width (desire)
I want to keep the Control Width to it's Content Width, how to I do that ?
HorizontalAlignment="Left"
This will keep the Control from stretching based on the container which it resides and instead will expand based on its contents if the Width is set to Auto.
MSDN provides an overview on Alignment, Margins, and Padding which may be of assistance.

Silverlight Get actual width of Grid that is set as Auto

Say I have a Grid whose Width is set as Auto. Inside this grid I want to put a new grid that is a square. To do this I want the new grid's width and height to be equal. I thought I could just set the Width and Height to the Parent Grid's Width. The problem is, the Parent grid's width is double.NaN since it is set to auto.
My question, is there a way to get the ActualWidth of this parent grid, so that I know how wide it is? If not, is there another way to create a Rectangular Grid that fits inside the parent grid?
You have to use the property of ActualWidth
var width = LayoutRoot.ActualWidth;
I am not sure how well it works with windows phone 7 but see if this article helps you.

WPF Window MinWidth depending on size of toolbar

Before asking this question I have looked at all related questions, but have not found anything relevant.
In my application I have toolbox style bar, which is basically stack panel with bunch of buttons. User may change which buttons are shown in toolbar.
Window width may be changed, but it can not be smaller then width of toolbar.
At first I was hoping to bind the MinWidth property to the StackPanel Width property and create converter that adds few pixels to Width of the StackPanel. The problem is that my converter does not get Width of StackPanel, just NaN as value :(
Unfortunately, StackPanel width is set to Auto and I can not change that.
Is there any way I can make my Window MinWidth dependable on Width of StackPanel?
Use ActualWidth, not Width.
Width is the requested width, or NaN for "Auto", ActualWidth is the, well, actual width after all the layout is calculated.

Resources