问题描述
如何将WPF数据网格中每个单元格的TextWrapping设置为 NoWrap?我知道单元格本身没有 TextWrapping属性,但是我想在单元格内的控件上设置该属性。
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.
我正在使用的DataGrid没有明确定义的列,其显示的结果集是动态的。
The DataGrid I am working with does not have columns defined explicitly, the result set it is displaying is dynamic.
我正在寻找与以下链接中提供的答案类似的解决方案。但是,我不想显式覆盖单元格样式/模板并定义要使用的控件。相反,我想说的是,如果正在使用TextBlock,请将其TextWrapping属性设置为NoWrap。
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.
推荐答案
在DataGrid的资源中,可以为TextBlocks指定替代的默认样式。这应该满足您的要求( 如果正在使用TextBlock,请将其TextWrapping属性设置为NoWrap )。如果TextBlocks明确指定了要使用的其他样式,则此方法将无效。
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>
(未经测试,因为我现在没有Visual Studio。)
(Untested, since I do not have Visual Studio available right now.)
这篇关于WPF DataGrid单元格文本包装-设置为NoWrap(假)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!