Friday, August 19, 2011

GridColumn does not stop children from expanding and the column expands with the children!!

The problem: So you have DataGrid Grid with a TextBox in a column .. when the user start typing some text it will expand ignoring all parent controls and all restrictions!! What is the solution?

The solution is: use the * for the column width instead of "Auto" in ColumnDefinition..

Details:
If grid column definition is set to * then it stretches to take all available space provided by parent. Yet it prevents children (and itself) from growing beyond the available space provided by parent.

while using "Auto" for column width will grow as necessary (by children) regardless how much space its parent gives it and ignoring all restrictions.

StackPanel cannot stretch properly

Context: I created a TextBox element and a button; both inside a StackPanel. And I put the StackPanel inside a Grid. I gave the StackPanel HorizontalAlignement.Stretch. And I don't want to give width to my TextBox .

Problem: My StackPanel is not expanding to fill the available space!!! even though I gave it Stretch for HorizontalAlignment.

Solution: use DockPanel.

Reasoning: StackPanel is designed to shrink as much as its children accept.