Silverlight adjust DataGrid height at run time
I have a Grid of 5 rows. At row 2 and 4 I have a DataGrid. The other rows have a fixed Height. The contents of the tables in the DataGrids is of course dynamically determined, and therefore their height too. As in my particular app there is a relation between the height of the two tables, I want to adjust the MaxHeight of both at run-time. If a table is longer than the initially speficied MaxHeight, it is supposed to get a ScrollBar. But the Height of both Tables should never be longer than the browser's page.
Now I tri开发者_Python百科ed to do this at runtime triggered at some event:
if (availableSpace - heightOfTable1 - heightOfTable2 < 0)
{
if (heightOfTable1 > heightOfTable2)
{
Table1.MaxHeight = availableSpace - heightOfTable2;
// Trigger a rerendering
Table1.ItemsSource = null;
Table1.ItemsSource = List1;
...
}
...
}
However I noticed that the ActualHeight is not changed when I do this. The ActualHeight is now bigger than the specified MaxHeight at run time What am I doing wrong?
Also set the datagrid Height properties to MaxHeight (if they exceed their current MaxHeight). It will not recheck against a MaxHeight change if it already exceeds it.
You should not need to trigger a re-render... Have you considered changing the row heights and max row height of the grid rows in code instead of the datagrids? Things seem to run a lot smoother when I manipulate rows heights, rather than the content height. It also makes sense in your example as you are basically trying to allocate the remaining row height (rows 2 & 4) on a calculated basis. Changing the datagrid sizes seems wrong.
Just name individual rows with x:Name="Row2" etc like you would any other element. I actually use databinding of grid row heights to calculated double properties (as I am using MVVM), but code-behind seems to be the place for your example.
精彩评论