自动高度与MaxHeight结合使用
我在设置以下xaml布局时遇到问题:
RowHeightAuto.xaml
DataGrid1控件未显示任何定义了大量列和行的滚动条。 当我用高度=“*”替换高度=“自动”时,一切都有效,而水平和垂直滚动条看起来像预期的那样。
当我直接在DataGrid1声明MaxHeight时它也可以工作,但这并不是我想要的。
这是一个错误,childcontrol在设置Height =“Auto”时忽略了最大高度,还是我可能做错了? 使用ListBox / ListView等可以重现相同的行为,也可以使用ComponentOne,Telerik等第三方控件…
如果这是一个错误 – 你知道一个解决方法或有其他提示吗?
以下是我如何设置DataGrid的ItemsSource的代码。 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 GetTestData() { const int maxCols = 501; const int maxRows = 300; var testDatas = new List(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; } }
这正如你所说的那样。
你没有看到Scrollbar
的原因是因为即使Grid
Clip是DataGrid
,它只是一个Clip, DataGrid
的ActualHeight
是允许显示它所有孩子的高度。 因此你没有看到它的滚动条。 ActualHeight
,它允许在Grid
上使用Height="Auto"
获得所需的所有空间。 我之所以不会将此称为个人错误,是因为如果您希望使用 考虑到这一点,我实际上认为我在“不理想的function”而不是“不正确的输出”方面也称这是一个错误 Grid
的ClipToBounds
属性进行某些动画,这可能是您想要的行为。
为了得到你想要的行为,
- 在
DataGrid
上应用MaxHeight
或使用GridRowDefinition.Height="*"
< - 如您所述(不确定为什么您说这不是您想要做的?) - 或者您也可以在
DataGrid
上使用RelativeSourceBinding
就像是 –
对于这些问题, Snoop是你的朋友。 您可以轻松检查此行为,并了解在使用Snoop检查DataGrid
上的ActualHeight
时未显示滚动条的原因,并查看它为子控件分配了更多高度。