Silverlight Get actual width of Grid that is set as Auto - silverlight

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.

Related

WPF - getting the REAL width of a control

I want to set the MinWidth of a Grid ColumnDefinitions to the ActualWidth of a DataGrid inside it.
The main problem is the DataGrid has a (built-in) vertical scroller since its contents are many. So, the actual width is the size of its columns and some more, but not including the scroller.
What happens is that when I set the MinWidth, I get the rightmost column hidden behind the scroller.
I appreciate solving this both in the code and the XAML (just in order to learn).
Try putting the DataGrid into a Canvas and bind to the ActualWidth property of that instead.

WinForms autosize vertically while leaving width adjustable?

Is it possible to use WinForms layout engine to set up a form in such a way that the height is adjusted completely automatically to fit the content, but the width would be adjustable by the user?
As an example, consider the following somewhat contrived form:
TLP: TableLayoutPanel, with 1 row and 2 columns.
label1: Label, contained in the left column. Set Text to something long, AutoSize to false, Dock to Fill.
button1: Button, contained in the right column. Set AutoSize to true.
Now set the row to "AutoSize", the first column to "100%" and the second column to "AutoSize".
Finally, set the whole TLP to AutoSize true, AutoSizeMode to GrowAndShrink.
At this point, the TLP could reasonably be resized horizontally, but WinForms doesn't seem to allow this.
The idea is that the form containing this TLP would grow or shrink vertically based on the width, which the user can change by resizing the window as usual.
Hopefully you have got the solution by now. But in case anybody having a same query, You can set width of Maximum Size Property to the Actual Width of the Form and the Height of MaximumSize to 0.
This is entirely possible. To do this, you have to override SetBoundsCore on your form. In this you can then control the height such that it doesn't change when a resize might want it to. Just store the height you want it to be in a variable and then have your override always set it to that. This way, when you auto-size the height, you save the new height aware before setting form Height and SetBoundsCore will honour that new height.

WPF expander size issue

I am using an ItemsControl to place n number of Expanders. My ItemsControl will be placed in a Grid with height 600 (for example). I want to make sure my ItemsControl will always occupy the entire available Height, to do so it can expand some of the Expanders.
What will be the best approach to do so?
One way would be to bind the Height and Width properties of your control to the parent's ActualWidth and ActualHeight properties

How to place a long Grid in a WPF Layout Container?

When I place a Datagrid in a WPF Grid Layout container, the datagrid lengthens the Grid Layout.
I want it to occupy only the space available on screen.
Make sure your Grid.ColumnDefinition or Grid.RowDefinition's Width or Height is not set to Auto. If you set it to a fixed size or a star size then the row/column should not resize.
If you're still having problems a code sample would be useful.
You should place your Datagrid into a ScrollViewer, and the ScrollViewer into your Grid cell.
the grid is placed inside a tabitem.
I tried the scrollviewer. but the only thing that happens:
the scrollviewer gets as large as the too long datagrid.
as the tabitem can vary in height, I would rather not like to specify a fixed height for the grid.

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