WPF DataGrid:如何将列设置为TextWrap? [英] WPF DataGrid: How do I set columns to TextWrap?

查看:221
本文介绍了WPF DataGrid:如何将列设置为TextWrap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我的代码没有正确的TextWrapping。它不包括描述列的文本(这是我想要的)。它只是切断它,它甚至不使用...来让我知道有更多的数据。

I'm not sure why my code isn't doing the TextWrapping properly. It doesn't wrap the text for the Description column (which is what I want). It just cuts it off and it doesn't even use the "..." to let me know there is more data.

我试图使用我发现的代码网上做这个工作,但是没有办法。理想情况下,我希望只能将TextWrap设置为某些列,而不是通用于所有DataGridCell对象。

I tried to use this code I found online to do the job, but it didn't work. Ideally I'd love to be able to only set TextWrap to certain columns and not generically across all DataGridCell objects.

哦,请注意,我正在使用Microsoft。 NET 4,所以这是通过这个提供的DataGrid,而不是WPF工具包。

Oh, and please do note that I am using Microsoft.NET 4 so this is the DataGrid offered through that, not from the WPF Toolkit.

<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看起来像:

It Doesn't work because the "Text" property of your TextBlock is actually being set to another object instead of just a string. At runtime, your VisualTree looks something like:

Cell
  - TextBlock (w/ TextWrapping and TextTrimming)
    -  ContainerVisual
       -  ContentPresenter
          -  TextBlock (auto-generated by the DataGrid)



<简而言之,你的代码基本上是这样的:

In short, you're code is essentially doing something like this:

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

要解决这个问题,请尝试更新您的ControlTemplate,如下所示:

To fix this, try updating your ControlTemplate as follows:

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

这篇关于WPF DataGrid:如何将列设置为TextWrap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆