如何以编程方式更改wpf DataGrid中所选行的行高?

时间:2011-06-13 05:53:17

标签: c# wpf

DependencyObject dep = (DependencyObject)e.OriginalSource;

while ((dep != null) && !(dep is DataGridRow))
{
    dep = VisualTreeHelper.GetParent(dep);
}

if (dep != null && dep is DataGridRow)
{
    DataGridRow row = (DataGridRow)dep;

    row.Height = 35;
}

使用此代码选择行的高度已更改但行内容高度未更改。

1 个答案:

答案 0 :(得分:3)

你也可以用风格来做,

<Style x:Key="PrettierDataGridStyle" TargetType="dg:DataGrid">
        <Setter Property="CellStyle">
            <Setter.Value>
                <Style TargetType="dg:DataGridCell">
                    <Style.Triggers>
                        <!-- Highlight selected rows -->
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="height" Value="35"></Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>