如何在所有列标题上启用文本换行?
如何在 DataGrid 的所有列标题上启用文本换行,而不禁用其他默认标题功能?例如需要的列大小调整、排序方向指示器等。
有没有办法做到这一点?
How does one enable text wrapping on all column headers of a DataGrid
, without disabling the other default header functionality? Such as column resizing, sort direction indicator, etc which are needed.
Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
或者不要理会 app.xaml 文件中的原语并执行以下操作(我的对象):
Or don't bother with the primitives in the app.xaml file and do the following (my objects):
您需要将以下命名空间添加到您的 app.xaml 文件中:
然后将此标记添加到 app.xaml 中:
这会将文本换行添加到整个 WPF 应用程序中的所有 DataGrid。
当我们讨论这个主题时,如果您只想将每个 DataGrid 标题的文本居中,您可以使用以下样式来实现此目的:
然而我已经看到这么多 WPF 文章建议您只能通过在每个 DataGrid 列上单独添加 TextWrapping 或 HorizontalAlignment 来实现此目的:
You need to add the following namespace to your app.xaml file:
and then add this tag to app.xaml:
This will add text-wrapping to the headers of all of your DataGrids throughout your WPF application.
While we're on the subject, if you just wanted to center the text of each of your DataGrid headers, you could do this using the following style instead:
Yet I've seen so many WPF articles suggesting that you can only do this by adding TextWrapping or HorizontalAlignment on each DataGrid column individually:
我将
添加到标题文本中我希望文本换行的位置。当您不需要将标题文本绑定到某些内容时,这非常有用
I added
to the header text where I want the text to wrap. This is useful when you don't need to bind your header text to something
我没有将列名称直接分配给 DataGridColumn.Header 属性,而是创建了一个包含列名称的 TextBlock,将 TextBlock 的 TextWrapping 属性设置为“Wrap”,并将 TextBlock 分配给 DataGridColumn.Header 属性。这保留了默认的标头功能。
例子:
Instead of assigning the column name directly to the DataGridColumn.Header property, I created a TextBlock containing the column name, set the TextWrapping property of the TextBlock to "Wrap" and assigned the TextBlock to the DataGridColumn.Header property. This preserves the default header functionality.
Example:
您可以为列标题创建全局
Style
。如果没有任何示例标记,我不知道语法,但它应该看起来像这样:由于
Style
是无键的,因此它将自动应用于所有列标题。并且样式不会覆盖任何本地设置的属性,因此它不会“禁用”任何现有的标头功能。You could create a global
Style
for your column headers. Without any example mark-up I don't know the syntax, but it should look something like this:Since the
Style
is key-less, it will automatically be applied to all of your column headers. And styles will not override any locally set properties, so it won't "disable" any existing header functionality.