1
votes

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
Do a view source in the browser and paste the code here so that we can look at the generated HTML.Registered User
I don't think I can do that because this is a WPF desktop project so no browser is used. I took a look around anyway to be sure and didn't find any generated HTML. Did I misunderstand you?Jesslyn

1 Answers

1
votes

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>