WPF: Set Control Height to Fill Grid Row Height - wpf

A WPF Grid control with 3 row and 3 columns. The Height of the Row in question is set to Auto. In the first two cells I have two controls with dynamic heights. In the third cell, I have another control I want to be automatically set to stretch within the Grid cell. I have tried VerticalAlignment="Stretch", but that simply sets Grid Row Height equal to control's height. What are my options here?

Not sure what kind of control you are using in Cell 3, but most of the WPF controls will automatically stretch to fit inside the Grid cell. The row height of your grid will be set by the height of the controls in Cells 1 and 2.
If you are using some kind of custom control where the default behavior for height is different, you can set Height="Auto".
If that doesn't work either, you can do a data binding to get the actual height of the control in either cell 1 or 2. Set the Height property of your control in Cell 3 to the following:
Height="{Binding ActualHeight, ElementName=MyControlNameFromCell1, Mode=OneWay}"
EDIT
Another way that might be more robust is to do a data binding for the Height of the row. So instead of using "Auto" for the height of the row, use the data binding shown above.

Related

autosize height of gridcontrol in list

I've got a winforms application, using devexpress library. In this application I have an overview form with a list of datagrids (gridview controls of devexpress). The number of rows is dynamic, so is the number of datagrids shown. The grids are programatically added to a panel on the form.
The width of the grids need to scale with the width of the form. I managed to do that by setting the property docking to "top". The height of the grids should scale based on the rows inside. But I can't make this happen.
How can I add a grid to the panel (or form) with width set to 100% and height based to content of the grid?
Refer this - Calculate the "perfect" size/height of a Grid
The height of the grid content can be calculated via the ViewInfo
object. Follow approach from the How to change the Grid's height
according to the total height of its rows example.

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.

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.

WPF, ShowGridLines equivalent for wrap panel

I need to display a 1 pixel wide border around all wrap panel cells, kinda like excel grid. Unfortunately the wrap panel does not implement the grid ShowGridLines property. I can't put a border inside every cell because adjacent cells will have a 2 pixel border instead of 1 pixel.
Since the wrap panel arranges it's layout dynamically and does not expose it's properties I can't evaluate the correct value for a border inside a cell. Any workaround possible?
There is no way to show grid lines because it's just not a grid. Even in a WPF grid control, you have very little control over the appearance of lines: How can I change the color of the gridlines of a Grid in WPF?. They're really just for debugging. The workaround is to put a border inside each cell that only has thickness on the bottom and on the right: BorderThickness="0,0,1,1"

How to set grid cursor can work in entire grid?

I have a grid with 2 rows * 2 columns, and put a TextBlock in left-up cell(row=0, column=0).
then I set Grid.Cursor = "Hand" in XAML. the hand-cursor displayed only when mouse over TextBlock. But I want to display the hand-cursor in entire grid.
How to do it?
Set the Background of the Grid to transparent.
The Cursor is only applying to the content area of the grid. Thus to get the cursor across the whole grid, fill it in.
This can be shown, by changing what grid cells the TextBlock is positioned in.

Resources