Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Lou,</p> <p>I too am struggling with this problem. There is a solution, but it doesn't seem to work so well on Windows XP. The trick is to reserve space for your DataGrid by creating a placeholder row that is empty. Move your DataGrid outside of the layout grid and set the margins as shown below.</p> <pre><code>&lt;Grid SizeChanged="Grid_SizeChanged"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="40" /&gt; &lt;RowDefinition Name="DetailGridRow" Height="*" /&gt; &lt;RowDefinition Height="40" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Button Grid.Row="0" Width="80" Content="Do Something" Margin="5" /&gt; &lt;Button Grid.Row="2" Width="80" Content="Do Something Else" Margin="5" /&gt; &lt;/Grid&gt; &lt;DataGrid Margin="0,40,0,40" Name="dgGrid" AutoGenerateColumns="True" /&gt; </code></pre> <p>Now place this SizeChanged handler is your code behind class.</p> <pre><code>/// &lt;summary&gt; /// In order to prevent the datagrid from growing vertically the size is set to an empty /// Grid row's size. /// &lt;/summary&gt; /// &lt;param name="sender"&gt;&lt;/param&gt; /// &lt;param name="e"&gt;&lt;/param&gt; private void Grid_SizeChanged(object sender, SizeChangedEventArgs e) { Grid layoutGrid = sender as Grid; double marginWidth = (layoutGrid.Margin.Left + layoutGrid.Margin.Right); double marginHeight = (layoutGrid.Margin.Top + layoutGrid.Margin.Bottom); double minHeight = dgGrid.MinHeight; if (e.HeightChanged) dgGrid.Height = DetailGridRow.ActualHeight &gt; minHeight ? DetailGridRow.ActualHeight - marginHeight : minHeight - marginHeight; if (e.WidthChanged) dgGrid.Width = layoutGrid.ActualWidth - marginWidth; e.Handled = false; } </code></pre> <p>As I mentioned earlier, this technique has problems on Windows XP. The problem that I run into is that the column widths are all messed up resulting in a grid that is unusable unless you find the "magic" column and double-click it's column divider, which restores all the column widths to their correct size. I'm still working out the solution to that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload