WPF DataGrid:如何将列设置为TextWrap?

WPF DataGrid:如何将列设置为TextWrap?,wpf,datagrid,word-wrap,Wpf,Datagrid,Word Wrap,我不知道为什么我的代码没有正确地进行文本包装。它没有包装描述列的文本(这正是我想要的)。它只是将其切断,甚至不使用“…”让我知道还有更多数据 我试着用我在网上找到的代码来做这项工作,但没有成功。理想情况下,我希望能够仅将TextWrap设置为某些列,而不是一般地跨所有DataGridCell对象 哦,请注意,我使用的是Microsoft.NET 4,所以这是通过它提供的数据网格,而不是WPF工具包 <DataGrid Name="TestGrid" Grid.Row="2" Grid.Co

我不知道为什么我的代码没有正确地进行文本包装。它没有包装描述列的文本(这正是我想要的)。它只是将其切断,甚至不使用“…”让我知道还有更多数据

我试着用我在网上找到的代码来做这项工作,但没有成功。理想情况下,我希望能够仅将TextWrap设置为某些列,而不是一般地跨所有DataGridCell对象

哦,请注意,我使用的是Microsoft.NET 4,所以这是通过它提供的数据网格,而不是WPF工具包

<DataGrid Name="TestGrid" Grid.Row="2" Grid.ColumnSpan="2" AutoGenerateColumns="False" ItemsSource="{Binding IntTypes}" SelectedValue="{Binding CurrentIntType}">
 <DataGrid.Resources>
  <Style TargetType="{x:Type DataGridCell}">
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="{x:Type DataGridCell}">
      <Border Name="DataGridCellBorder">
       <TextBlock Background="Transparent" TextWrapping="WrapWithOverflow" TextTrimming="CharacterEllipsis" Height="auto" Width="auto">
        <ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}"  ContentTemplate="{TemplateBinding Property=ContentControl.Content}" />
       </TextBlock>
      </Border>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>
 </DataGrid.Resources>
 <DataGrid.Columns>
  <DataGridTextColumn Header="ID" Binding="{Binding ID}" IsReadOnly="True" />
  <DataGridTextColumn Header="Interested Parties Description" Binding="{Binding Description}" IsReadOnly="False"  />
 </DataGrid.Columns>
</DataGrid>


提前谢谢

它不起作用,因为TextBlock的“Text”属性实际上被设置为另一个对象,而不仅仅是一个字符串。在运行时,您的VisualTree看起来像:

Cell
  - TextBlock (w/ TextWrapping and TextTrimming)
    -  ContainerVisual
       -  ContentPresenter
          -  TextBlock (auto-generated by the DataGrid)
简而言之,您的代码基本上是这样做的:

<TextBlock TextTrimming="CharacterEllipsis" TextWrapping="WrapWithOverflow">
  <TextBlock Text="The quick brown fox jumps over the lazy dog"/>
</TextBlock>

要解决此问题,请尝试按如下方式更新ControlTemplate:

<ControlTemplate TargetType="{x:Type DataGridCell}">
    <Border Name="DataGridCellBorder">
        <ContentControl Content="{TemplateBinding Content}">
            <ContentControl.ContentTemplate>
                <DataTemplate>
                    <TextBlock Background="Transparent" TextWrapping="WrapWithOverflow" TextTrimming="CharacterEllipsis" 
                                Height="auto" Width="auto" Text="{Binding Text}"/>
                </DataTemplate>
            </ContentControl.ContentTemplate>
        </ContentControl>
    </Border>
</ControlTemplate>