I am facing a problem with setting the following xaml layout:
RowHeightAuto.xaml
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="GridMaxHeight.RowHeightAuto"
Title="RowHeightAuto" WindowState="Maximized">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" MaxHeight="200" />
</Grid.RowDefinitions>
<StackPanel Background="LightGray" Grid.Row="0"></StackPanel>
<DataGrid Name="DataGrid1" Grid.Row="1" />
</Grid>
The DataGrid1 control isn't showing any scrollbars with a lot of columns and rows defined.
Everything works find when I would replace the Height="Auto" with Height="*" than Horizontal and Vertical scrollbars appear like expected.
Also it works when I would declare the MaxHeight directly at the DataGrid1, but that's not really want I want.
Is this a bug that the childcontrol ignores the maxheight on setting Height="Auto" or am I propably making something wrong? Same behaviour can be reproduced with ListBox/ListView and so on, also with third party controls like ComponentOne, Telerik...
If it's a bug - do you know a workaround or have other hints for me?
Here is the code how I set the ItemsSource of the DataGrid.
RowHeightAuto.xaml.cs
public partial class RowHeightAuto : Window
{
private readonly DateTime _start;
public RowHeightAuto()
{
InitializeComponent();
DataGrid1.ItemsSource = GetTestData();
_start = DateTime.Now;
Dispatcher.BeginInvoke(new Action(() => MessageBox.Show((DateTime.Now - _start).TotalSeconds.ToString(CultureInfo.InvariantCulture))), DispatcherPriority.ContextIdle, null);
}
public static List<TestData> GetTestData()
{
const int maxCols = 501;
const int maxRows = 300;
var testDatas = new List<TestData>(maxRows);
for (int i = 0; i < maxRows; i++)
testDatas.Add(new TestData());
for (int i = 0; i < maxCols; i++)
{
string propName = string.Format("Property{0}", AddLeadingZeros(i));
for (int j = 0; j < maxRows; j++)
testDatas[j][propName] = propName;
}
return testDatas;
}
private static string AddLeadingZeros(int val)
{
return val.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0');
}
}
public class TestData
{
public object this[string propertyName]
{
get
{
var myType = GetType();
var myPropInfo = myType.GetProperty(propertyName);
return myPropInfo.GetValue(this);
}
set
{
var myType = GetType();
var myPropInfo = myType.GetProperty(propertyName);
myPropInfo.SetValue(this, value, null);
}
}
public string Property000 { get; set; }
public string Property001 { get; set; }
public string Property002 { get; set; }
public string Property003 { get; set; }
...
public string Property498 { get; set; }
public string Property499 { get; set; }
public string Property500 { get; set; }
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…