C# 以编程方式设置DataGrid行高属性

C# 以编程方式设置DataGrid行高属性,c#,wpf,datagrid,C#,Wpf,Datagrid,关于.NET4.0中的标准WPF数据网格,我有一个问题 当我尝试使用简单代码设置DataGrid网格行高程序时: private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e) { e.Row.Height = 120; } 一切都很顺利,直到我尝试在用户界面上调整网格行的大小/使用excel/中类似鼠标的标准方式-然后网格行似乎无法调整大小。一直保持在120。顺便说一下,它的内容

关于.NET4.0中的标准WPF数据网格,我有一个问题

当我尝试使用简单代码设置DataGrid网格行高程序时:

private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.Height = 120;            
}
一切都很顺利,直到我尝试在用户界面上调整网格行的大小/使用excel/中类似鼠标的标准方式-然后网格行似乎无法调整大小。一直保持在120。顺便说一下,它的内容都搞砸了


就像西尼亚德·奥康纳说的:告诉我,宝贝,我哪里做错了

您不需要设置行本身的高度,因为它是通过页眉等调整大小的。有一个属性,
DataGrid.RowHeight
,允许您正确执行此操作

如果需要有选择地设置高度,可以创建样式并将
DataGridCellsPresenter
的高度绑定到项目的某些属性:


或者,您可以从可视化树中获取演示者(我不建议这样做),并在那里指定高度:

// In LoadingRow the presenter will not be there yet.
e.Row.Loaded += (s, _) =>
    {
        var cellsPresenter = e.Row.FindChildOfType<DataGridCellsPresenter>();
        cellsPresenter.Height = 120;
    };
这对我有用

private void SetRowHeight(double height)
{
    Style style = new Style();
    style.Setters.Add(new Setter(property: FrameworkElement.HeightProperty, value: height));
    this.RowStyle = style;
}
private void SetRowHeight(double height)
{
    Style style = new Style();
    style.Setters.Add(new Setter(property: FrameworkElement.HeightProperty, value: height));
    this.RowStyle = style;
}