Data grid height is too tall when using word-wrap in text block column

霸气de小男生 提交于 2020-01-05 07:49:17

问题


I desire to make data grid with one column representing rather lengthy text values. So my goal functionality is:

  • Column width to be as wide as remaining window space (window can be re-sized)
  • Word-wrap text as necessary
  • Limit data grid height to remaining height of window and provide vertical scroll as necessary

The following code meets the first two items and provides a functioning vertical scroll bar, but the data grid height is bizarrely too tall for the content it is showing. Removing word-wrap from the text block fixes this... but I need the word-wrap.

How can I keep the word-wrap functionality without the data grid height getting too excessive?

<ScrollViewer VerticalScrollBarVisibility="Auto">
   <Grid>
      <Grid.ColumnDefinitions>
         <ColumnDefinition Width="Auto" />
         <ColumnDefinition Width="*"/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
         <RowDefinition Height="Auto" />
         <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>
      <!-- other controls in different parts of the data grid -->
      <DataGrid Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left"
         Margin="0,6,6,6" Name="dgMessages" VerticalAlignment="Top" 
         Background="DarkGray" HeadersVisibility="None"
         AlternatingRowBackground="Gainsboro" CanUserResizeColumns="False"
         CanUserResizeRows="False" CanUserSortColumns="False" 
         AutoGenerateColumns="false" BorderBrush="Black" HorizontalGridLinesBrush="{x:Null}"
         ItemsSource="{Binding Messages, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
             <DataGrid.Columns>
                 <dg:DataGridTemplateColumn Width="*">
                     <dg:DataGridTemplateColumn.CellTemplate>
                         <DataTemplate>
                             <TextBlock Text="{Binding Value}" 
                                TextWrapping="WrapWithOverflow" 
                                Padding="5,5,5,5" />
                         </DataTemplate>
                     </dg:DataGridTemplateColumn.CellTemplate>
                 </dg:DataGridTemplateColumn>
             </DataGrid.Columns>
         </DataGrid>
     </Grid>
 </ScrollViewer>

"dg" namespace is "http://schemas.microsoft.com/winfx/2006/xaml/presentation"


回答1:


Removing the scrollviewer and setting the height of the row containing the data grid to "*" fixed this. Wrapping text in the data grid column is treated normally when the data grid is not in a row that is automatically sized to content.

Here is the end code:

<Grid>
   <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="*"/>
   </Grid.ColumnDefinitions>
   <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
   </Grid.RowDefinitions>
   <!-- other controls in different parts of the data grid -->
   <DataGrid Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left"
      Margin="0,6,6,6" Name="dgMessages" VerticalAlignment="Top" 
      Background="DarkGray" HeadersVisibility="None"
      AlternatingRowBackground="Gainsboro" CanUserResizeColumns="False"
      CanUserResizeRows="False" CanUserSortColumns="False" 
      AutoGenerateColumns="false" BorderBrush="Black" HorizontalGridLinesBrush="{x:Null}"
      ItemsSource="{Binding Messages, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
      <DataGrid.Columns>
         <dg:DataGridTemplateColumn Width="*">
            <dg:DataGridTemplateColumn.CellTemplate>
               <DataTemplate>
                  <TextBlock Text="{Binding Value}" TextWrapping="WrapWithOverflow" Padding="5,5,5,5" />
               </DataTemplate>
            </dg:DataGridTemplateColumn.CellTemplate>
         </dg:DataGridTemplateColumn>
      </DataGrid.Columns>
   </DataGrid>
</Grid>


来源:https://stackoverflow.com/questions/10857530/data-grid-height-is-too-tall-when-using-word-wrap-in-text-block-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!