WPF DataGrid 单元格文本换行 - 设置为 NoWrap (False)

发布于 2024-10-03 12:23:00 字数 540 浏览 0 评论 0原文

如何将 WPF DataGrid 中每个单元格的 TextWrapping 设置为“NoWrap”?我知道单元格本身没有“TextWrapping”属性,但我想在单元格内的控件上设置该属性。

我正在使用的 DataGrid 没有显式定义的列,它显示的结果集是动态的。

我正在寻找类似于以下链接中提供的答案的解决方案。但是,我不想显式覆盖单元格样式/模板并定义要使用的控件。相反,我想说,如果正在使用 TextBlock,请将其 TextWrapping 属性设置为 NoWrap。

WPF 工具包数据网格单元格文本换行
如何在所有列上启用文本换行列标题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2024-10-10 12:23:00

在 DataGrid 的资源中,您可以为 TextBlock 指定替代默认样式。这应该可以满足您的要求(“如果正在使用 TextBlock,请将其 TextWrapping 属性设置为 NoWrap”)。如果 TextBlock 明确指定要使用的不同样式,则这将不起作用。

<DataGrid ...>
    <DataGrid.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextWrapping" Value="NoWrap"/>
        </Style>
    </DataGrid.Resources>
    ...
</DataGrid>

(未经测试,因为我现在没有可用的 Visual Studio。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文