如何阻止WPF DataGrid TextColumn单元因换行符而变得越来越高?

时间:2013-02-07 23:53:12

标签: wpf wpfdatagrid

背景

我有一个带有多个TextColumns的DataGrid。一列是只读的,并绑定到DataGrid外部的TextBox文本。此TextBox是一个多行文本框,表示AcceptsReturn = true和TextWrapping = Wrap。

问题:

当用户在多行TextBox中输入多行时,绑定的DataGridCell(及其行)将垂直增长,以便显示所有文本。

问题:

  1. 当出现多行时,是否有办法让单元格中出现垂直滚动条?
  2. 通常,由于多行内容,有哪些其他技术可以规避DataGridCell及其行的增长?
  3. 研究

    我知道我可以通过强制设置其高度来阻止DataGridRow的增长。但是,这不会触发滚动条。

    我确认DataWridCell中的TextBlock已关闭WordWrap。

    由于

2 个答案:

答案 0 :(得分:3)

您是否尝试在其中使用DataGridTemplateColumnScrollViewer?例如:

<DataGridTemplateColumn Header="MyText">
 <DataGridTemplateColumn.CellTemplate>
  <DataTemplate>
   <ScrollViewer MaxHeight="30" VerticalScrollBarVisibility="Auto">
    <TextBlock TextWrapping="Wrap" Text="{Binding YourText}" />
   </ScrollViewer>
  </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

答案 1 :(得分:0)

我最终对我的要求有点灵活,并提出了这种风格:

<Style
    TargetType="{x:Type DataGridCellsPresenter}"
    BasedOn="{StaticResource {x:Type DataGridCellsPresenter}}"
    >
    <Setter
        Property="MaxHeight"
        Value="25"
        />
</Style>

通过在我的DataGridCellsPresenters上设置MaxHeight,我可以触发垂直滚动条而不会直接干扰行高。我可以在我的应用程序中一致地应用它,而不是按列。