简体   繁体   中英

Fix for WPF DataGrid column headers cut off?

In my WPF app I use a DataGrid, and when I have columns whose header label text is close to the width of the column, the right part of the column header text gets cut off by a region of blankness on the right side of the header area, which is like internal padding just on the right side of the header that is 6 or 8 pixels wide, which seems to have no good reason.

I searched around for similar questions and saw someone mentioning a 0,6, 0,6 padding value that cannot be changed, but I don't know if they were talking about the same thing I am seeing.

Anyway, I would love for someone who knows a workaround to let me know what it is.

Thanks!

In .NET 4, the DataGridColumnHeader wraps the header content in an instance of DataGridHeaderBorder . The DataGridHeaderBorder does appear to add a padding of "3,3,3,3" around the content, if it's Padding property is currently set to "0,0,0,0" .

There is additional logic based on whether the DataGridHeaderBorder is being used for the column or row headers. In addition, the various themes have similar, but not exactly the same, logic.

The only way to prevent DataGridHeaderBorder from adding it's padding is to specify your own. So you can use:

<Style TargetType="DataGridColumnHeader">
    <Setter Property="Padding" Value="1,0" />
</Style>

Or anything other than all zeros, such as "0.0001" or "-1" . The DataGridColumnHeader will pass it's padding on to the DataGridColumnHeader , which is why you can use the implicit style above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM