我对.NET 4.0中的标准WPF DataGrid有一个问题。
当我尝试使用简单的代码以编程方式设置DataGrid网格行高时:
private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Height = 120;
}
Run Code Online (Sandbox Code Playgroud)
一切正常,直到我尝试像使用excel /一样在一侧使用鼠标在用户界面/ standard方式上调整网格行的大小-然后看来网格行无法调整大小。它一直保持120。顺便说一句,它的内容混乱了……
就像Sinead O'Connor会说:告诉我,宝贝-我哪里出了问题?
您无意设置行本身的高度,因为它会通过标题等来调整大小。有一个属性,DataGrid.RowHeight可让您正确执行此操作。
如果您需要有选择地设置高度,则可以创建样式并将的高度绑定DataGridCellsPresenter到商品的某些属性:
<DataGrid.Resources>
<Style TargetType="DataGridCellsPresenter">
<Setter Property="Height" Value="{Binding RowHeight}" />
</Style>
</DataGrid.Resources>
Run Code Online (Sandbox Code Playgroud)
或者,您可以从视觉树中获取演示者(我不建议这样做),并在其中指定高度:
// In LoadingRow the presenter will not be there yet.
e.Row.Loaded += (s, _) =>
{
var cellsPresenter = e.Row.FindChildOfType<DataGridCellsPresenter>();
cellsPresenter.Height = 120;
};
Run Code Online (Sandbox Code Playgroud)
FindChildOfType扩展方法在哪里可以这样定义:
public static T FindChildOfType<T>(this DependencyObject dpo) where T : DependencyObject
{
int cCount = VisualTreeHelper.GetChildrenCount(dpo);
for (int i = 0; i < cCount; i++)
{
var child = VisualTreeHelper.GetChild(dpo, i);
if (child.GetType() == typeof(T))
{
return child as T;
}
else
{
var subChild = child.FindChildOfType<T>();
if (subChild != null) return subChild;
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2881 次 |
| 最近记录: |