2013-02-07 28 views
0

背景:如何阻止WPF DataGrid TextColumn單元由於換行而變長?

我有一個DataGrid與多個TextColumns。一列是隻讀的,並綁定到DataGrid外部的TextBox的文本。這個TextBox是一個多行文本框,意思是AcceptsReturn = true和TextWrapping = Wrap。

問題:

當用戶進入在多行文本框多行,結合的DataGridCell(及其排)垂直增長,因此所有顯示的文本。

問題:

  1. 有沒有辦法得到一個垂直滾動條在細胞內出現,當多行應該出現?
  2. 一般來說,由於多行內容,其他技術可以規避DataGridCell及其行的增長?

研究:

我知道我可以防止DataGridRow的成長強行設置其高度。但是,這不會觸發滾動條。

而且我確認WordWrap已關閉DataGridCell內的TextBlock。

感謝

回答

0

我結束了幾分靈活的與我的要求和這種風格想出了:

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

通過對我的DataGridCellsPresenters設置MaxHeight,我可以觸發垂直滾動條,而不直接干擾行高。我可以在整個應用程序中而不是按列應用它。

3

您是否嘗試過使用DataGridTemplateColumnScrollViewer裏面呢?例如:

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