如何在所有列标题上启用文本换行?

发布于 2024-08-02 06:50:01 字数 87 浏览 2 评论 0原文

如何在 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 技术交流群。

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

发布评论

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

评论(5

哆啦不做梦 2024-08-09 06:50:01

或者不要理会 app.xaml 文件中的原语并执行以下操作(我的对象):

<DataGrid Name="WBdataGrid" AutoGenerateColumns="False" ColumnHeaderHeight="50" >
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.ColumnHeaderStyle>
    <DataGrid.Columns> ...

Or don't bother with the primitives in the app.xaml file and do the following (my objects):

<DataGrid Name="WBdataGrid" AutoGenerateColumns="False" ColumnHeaderHeight="50" >
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.ColumnHeaderStyle>
    <DataGrid.Columns> ...
眼藏柔 2024-08-09 06:50:01

我想启用文本换行
在我的 DataGrid 的所有列标题上,
不禁用其他默认值
标头功能

您需要将以下命名空间添加到您的 app.xaml 文件中:

xmlns:primitives="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"

然后将此标记添加到 app.xaml 中:

<Style TargetType="{x:Type primitives:DataGridColumnHeader}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

这会将文本换行添加到整个 WPF 应用程序中的所有 DataGrid。

当我们讨论这个主题时,如果您只想将每个 DataGrid 标题的文本居中,您可以使用以下样式来实现此目的:

<Style TargetType="{x:Type primitives:DataGridColumnHeader}">
    <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>

然而我已经看到这么多 WPF 文章建议您只能通过在每个 DataGrid 列上单独添加 TextWrapping 或 Horizo​​ntalAlignment 来实现此目的:

<toolkit:DataGridTextColumn Binding="{Binding Path=TaxAmount}">
    <toolkit:DataGridTextColumn.HeaderTemplate >
        <DataTemplate>
            <TextBlock Text="Tax Amount" Width="90" TextWrapping="Wrap" HorizontalAlignment="Center"/>
        </DataTemplate>
    </toolkit:DataGridTextColumn.HeaderTemplate>
</toolkit:DataGridTextColumn>

I would like to enable text wrapping
on all column headers of my DataGrid,
without disabling the other default
header functionality

You need to add the following namespace to your app.xaml file:

xmlns:primitives="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"

and then add this tag to app.xaml:

<Style TargetType="{x:Type primitives:DataGridColumnHeader}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

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:

<Style TargetType="{x:Type primitives:DataGridColumnHeader}">
    <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>

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:

<toolkit:DataGridTextColumn Binding="{Binding Path=TaxAmount}">
    <toolkit:DataGridTextColumn.HeaderTemplate >
        <DataTemplate>
            <TextBlock Text="Tax Amount" Width="90" TextWrapping="Wrap" HorizontalAlignment="Center"/>
        </DataTemplate>
    </toolkit:DataGridTextColumn.HeaderTemplate>
</toolkit:DataGridTextColumn>
傾城如夢未必闌珊 2024-08-09 06:50:01

我将 添加到标题文本中我希望文本换行的位置。当您不需要将标题文本绑定到某些内容时,这非常有用

<DataGridTextColumn x:Name="cAccountNumber" 
                    Header="Account
 Number" 
                    Width="80" 
                    Binding="{Binding Path=AccountNumber}" />

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

<DataGridTextColumn x:Name="cAccountNumber" 
                    Header="Account
 Number" 
                    Width="80" 
                    Binding="{Binding Path=AccountNumber}" />
皇甫轩 2024-08-09 06:50:01

我没有将列名称直接分配给 DataGridColumn.Header 属性,而是创建了一个包含列名称的 TextBlock,将 TextBlock 的 TextWrapping 属性设置为“Wrap”,并将 TextBlock 分配给 DataGridColumn.Header 属性。这保留了默认的标头功能。

例子:

<toolkit:DataGridTextColumn Binding="{Binding Path=MyProperty}">
    <toolkit:DataGridTextColumn.Header>
        <TextBlock Text="Something Longer" TextWrapping="Wrap" />
    </toolkit:DataGridTextColumn.Header>
</toolkit:DataGridTextColumn>

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:

<toolkit:DataGridTextColumn Binding="{Binding Path=MyProperty}">
    <toolkit:DataGridTextColumn.Header>
        <TextBlock Text="Something Longer" TextWrapping="Wrap" />
    </toolkit:DataGridTextColumn.Header>
</toolkit:DataGridTextColumn>
魂牵梦绕锁你心扉 2024-08-09 06:50:01

您可以为列标题创建全局Style。如果没有任何示例标记,我不知道语法,但它应该看起来像这样:

<Style TargetType="{x:Type dg:ColumnHeader}">
    <Setter Property="TextWrapping" Value="Wrap"/>
</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:

<Style TargetType="{x:Type dg:ColumnHeader}">
    <Setter Property="TextWrapping" Value="Wrap"/>
</Style>

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.

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