问题
How do I set the TextWrapping of every cell in a WPF DataGrid to "NoWrap"? I understand the Cell itself does not have a "TextWrapping" property, but I'd like to set the property on the control within the cell.
The DataGrid I am working with does not have columns defined explicitly, the result set it is displaying is dynamic.
I am looking for a solution similar to the answers provided in the links below. However I do not want to explicitly override the cell style/template and define the control to be used. Instead I would like to say, IF a TextBlock is being used, set its TextWrapping property to NoWrap.
WPF toolkit datagrid cell text wrapping
How do I enable text wrapping on all column headers?
回答1:
In the resources of your DataGrid, you can specify an alternative default style for TextBlocks. This should do what you require ("IF a TextBlock is being used, set its TextWrapping property to NoWrap"). This won't work if the TextBlocks explicitly specify a different style to be used.
<DataGrid ...>
<DataGrid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="NoWrap"/>
</Style>
</DataGrid.Resources>
...
</DataGrid>
(Untested, since I do not have Visual Studio available right now.)
来源:https://stackoverflow.com/questions/4197947/wpf-datagrid-cell-text-wrapping-set-to-nowrap-false