wpf 如何在 DataGridTextColumn 中实现文本换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24683640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:07:18  来源:igfitidea点击:

How to implement Text Wrapping within a DataGridTextColumn

wpfxaml

提问by Dennis O J

First off I am using WPF and DataGrid because I want Column Headers. However I am not married to this solution if there is a way to do this and still have column headers.

首先,我使用 WPF 和 DataGrid,因为我想要列标题。但是,如果有办法做到这一点并且仍然有列标题,我不会与此解决方案结婚。

Now I have done some extensive searching and tried various solutions that I found via googling but none of these seem to work or are not directly targeted at the issue I am having and further do not seem to be translatable either.

现在我进行了一些广泛的搜索并尝试了通过谷歌搜索找到的各种解决方案,但这些解决方案似乎都不起作用,或者没有直接针对我遇到的问题,而且似乎也无法翻译。

*I tried the following:

*我尝试了以下方法:

<Style TargetType="DataGridCell" x:Key="LeftColumnStyle">
    <Setter Property="FrameworkElement.HorizontalAlignment" Value="Left"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
</Style>

<DataGridTextColumn Header="Comments" HeaderStyle="{StaticResource LeftHeaderStyle}" CellStyle="{StaticResource LeftColumnStyle}" Binding="{Binding Path=Comments, UpdateSourceTrigger=PropertyChanged}" Width="250" />    

*But the wrapping did not work -- so I tried the following:

*但是包装不起作用——所以我尝试了以下方法:

<DataGridTextColumn Header="Comments" HeaderStyle="{StaticResource LeftHeaderStyle}" Binding="{Binding Path=Comments, UpdateSourceTrigger=PropertyChanged}" Width="250" >
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
            <Setter Property="TextBlock.TextAlignment" Value="Left"/>
            <Setter Property="TextBlock.FontSize" Value="14"/>
            <Setter Property="TextBlock.Width" Value="250"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

*The column wraps but does not auto-size in height so it simply seems to be truncating

*列会环绕但不会自动调整高度大小,因此它似乎只是在截断

I tried putting a Textblock directly within the DataGridTextColumn and bind the data to the Textblock but the DataGridTextColumn gives an error saying that it does not support direct content.

我尝试将 Textblock 直接放在 DataGridTextColumn 中并将数据绑定到 Textblock,但 DataGridTextColumn 给出一个错误,指出它不支持直接内容。

So in a nutshell I am trying to word wrap the columns that have text that will go beyond the preset cell width (I have more than one of these -- Description and Comments along with 5 other cells that do not wrap because they never exceed the preset cell width) however I cannot seem to get the height to adjust accordingly. I am okay with all the columns expanding in height for a row if either/both of the wrapping columns need more height much like that which would happen in an excel spreadsheet but I cannot even get the specific columns to expand when word wrapping. Thanks in advance.

因此,简而言之,我试图对包含超出预设单元格宽度的文本的列进行自动换行(我有多个 - 描述和注释以及其他 5 个不换行的单元格,因为它们永远不会超过预设单元格宽度)但是我似乎无法相应地调整高度。如果任一/两个换行列都需要更高的高度,就像在 excel 电子表格中发生的那样,但我什至无法在自动换行时扩展特定列,我可以接受所有列的高度扩展。提前致谢。

回答by NAJ

I had the same question earlier today and solved it by the following:

我今天早些时候遇到了同样的问题,并通过以下方式解决了它:

<DataGridTextColumn.ElementStyle>
    <Style>
        <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
    </Style>
</DataGridTextColumn.ElementStyle>