WPF DataGrid Cell Text Wrapping - set to NoWrap (False)

不问归期 提交于 2019-12-02 08:04:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!