Grid row height adjustment - c#

I have two Expanders in a grid, both in Auto sized rows.
When the height of the Expander is very large, the row overflows out of the grid.
Is there a way to have Auto sized rows that take only the grid space that's available?

Try setting the MaxHeight property on either the expander object or the object inside the expander.

You could use a DockPanel instead - and then dock the first Bottom and let the other fill. Or put each Expander inside a ScrollViewer.

i would set the last rowDefinition to Height="*",
then the last row fills out all the space left.
but it doesnt help if not only the last row would overflow..

See my custom auto-sizing panel/grid class here. I wrote it specifically to address this problem--full source is in the question.
(To use it, create a new class that inherits from panel and paste the working code into the class, then use the custom class just like a StackPanel.)
It currently only supports Vertical orientation, but could easily be modified to support Horizontal as well.

Related

Customize WPF application user controls

I'm trying to put in my windows a Datagrid and some other things (TextBox, Dropbox).
My problem is that when I resize the windows I can't control the items.
What I want to do is to devide the Windows like the picture below, and want the them to fit the window when I resize the window.
What do you suggest.
Put them in a Grid with two RowDefinitions. One for the toolbar/controls at the top, the other for the DataGrid.
Make the first row height Auto, and the second row height *.
Set the HorizontalAlignment and VerticalAlignment properties on the DataGrid to Stretch.
You should definitely use the Grid class.
Add Rows and Columns and change the Height and Width properties of these object to resize as you want (fixed value, Auto, *).

how to change width of a ListView when the form width increases?

I have a simple form with 2 ListViews. When I run the program it opens in default size for form and also for two listviews:
http://img838.imageshack.us/img838/6123/form1default.png
What I need to do is when the form gets expanded (only in WIDTH, I want the height be fixed) the second (wider) listview also be increased in width:
http://img269.imageshack.us/img269/4879/form1widthincrease.png
Can you pleae tell me what properties of form itself and/or second listview I have to change to achive this? Maybe some Events should be considered to add also?
Thanks!
You can achieve this by setting the Anchor property of the second ListView to include AnchorStyles.Right, e.g. Top, Left, Right in the properties window in Visual Studio.
You might also want to include AnchorStyles.Bottom, to resize the ListView(s) when the height of the form changes.
Better you put the two list views in a SplitContainer control and adjust the widths of two listviews. Use the Anchor property of the SplitContainer to increase its width according to the form.

Getting an Infragistics UltraWinGrid to resize height when adding or removing rows

I am looking for the best approach to getting the vertical height of a UltraWinGrid to change based on the number of rows it is bound to such that all rows are visible with no scroll bar. The intention is to stack several grids vertically in a scroll panel so only a single panel scroll bar appears when necessary rather than a scroll bar for each grid.
Basically the approach would depend on the current settings of your UltraGrid. Assuming that you have all rows with the same height than you could use something like the following:
int gridHeight = ultraGrid1.Rows.Count *ultraGrid1.DisplayLayout.Override.DefaultRowHeight + ultraGrid1.DisplayLayout.Bands[0].Columns[0].Header.Height + ultraGrid1.DisplayLayout.Bands[0].Header.Height + someConst;
Where the Column header and the Band header might not present into your layout. The
"someConst" refering to the "BorderStyleRow" property becasue the row border should also be taken into account when calculating the UltraGrid height. One more thing that you would like to consider would be the height of the GroupByBox if it is visible in your UltraGrid.
If this approach does not work for you please provide me more details of exact settings in your UltraGrid so I will try to assist you further with this matter.
The following post has a sample that gets the height of a UltraWinGrid and may be a good starting point for you:
http://blogs.infragistics.com/forums/p/18548/321187.aspx#321187

wpf: resizing a control according to its content

I have a control that inherits from Grid, it is a grid of hexagons that are generated dynamically according to the properties.
each of the hexagons is a button and a child of the Grid, and they have a style that displays them as hexagons.
what I want is for the grid to change its size according to the total size of the hexagons.
(I can calculate the exact size needed, but I don't know how to set it).
Basically you've got several options. A simple one is calculating the size yourself and assigning to the Grid's Width and Height.
A more elaborate solution would be to ask yourself a question: which layout is needed for my items? There are some standard containers which do the layout themselves and can grow/shrink with the content. For example, if your objects are just aligned in a line, you can go for StackPanel.

How to get the Width/Height of a collapsed control in WPF?

im wondering if theres an easy way to get the width of a control in WPF at runtime while the control is collapsed.
when i use control.Width ill get the following result: -1.#IND
and control.actualWidth will return 0.0 because its collapsed.
i want to resize my window and then display the collapsed control.
thanks
Edit:
Some details
i have a grid with 2 columns in my window, the 1st column holds a tab control, the 2nd column holds an expander control. i want to extend the width of my window when expanding the expander control, so the content in the 1st column will remain its size.
Put the control in question inside a container (like a ContentControl) and collapse the container rather than the control itself. Then you should be able to simply call Measure (or use the DesiredSize property) on the control to determine how much room it wants.
What size do you expect to get?
The size is not just dependent on the control but also on its container. So the actual size can not be determined unless the control is actually rendered.
Instead of using Collapsed you could make it Invisible that way it will be sized by its own logic and the logic of the container.
EDIT
In the comments it became clear that what the reason was for needing the size of the control:
I have a grid with 2 columns in my
window, the 1st column holds a tab
control, the 2nd column a holds an
expander control. i want to extend the
width of my window when expanding the
expander control, so the content in
the 1st column will remain its size.
My answer:
Set the SizeToContent of the window to WidthAndHeight and set the width of both grid columns to auto. That should take care of it.
I believe you're going about this the wrong way. You can set the Window Width and height to "Auto" and then it will take care of all the resizing stuff.
The problem arises whenever you directly set the Width property of any control(trust me I've done it). Once you do that, you've told WPF hands off of resizing logic, I know what I'm doing.
If you think something isn't resizing at the right time you can add a handler to some event and then call control.InvalidateVisual() or control.InvalidateMeasurement() which will make it go through a whole new layout pass.
You have to call the UpdateLayout method on the control or conainer of control. After that the things may work properly.
In UWP you can determine size of collapsed control by making it visible for a sec and then hiding it again, change is not noticeable:
var oldVisibility = myBorder.Visibility;
myBorder.Visibility = Visibility.Visible;
myBorder.UpdateLayout();
var height = myBorder.RenderSize.Height;
myBorder.Visibility = oldVisibility;
Post which is marked as an answer actually does not answer the question, it just gives a workaround.
To get the size of the collapsed control you need:
Set control's visibility as Hidden (Collapsed won't evaluate).
Call Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)) method of the control.
Get the size from DesiredSize property of the control.
Then you can Collapse your control back.

Categories