Search code examples
c#wpfdatagridscrollviewer

C# WPF datagrid row height won't exceed datagrid height


In C# and WPF and Visual Studio I am displaying some data from a sql database in a datagrid, in the description column it might have many many lines, and currently it will truncate display of a line rather than add a scroll. when there are multiple lines it will still truncate each line at max of parent datagrid height . problem shown here

below is what I've tried so far(this is why the screenshot has 2 description columns shown). adding scrollviewer to the datatemplate makes a greyed out scrollbar show, but nothing else.

          <DataGrid  VerticalAlignment="Top"  MaxHeight="225" Height="225" SelectionUnit="FullRow" MouseDoubleClick="Loaditem" AutoGenerateColumns="False" IsReadOnly="True" x:Name="grid" Margin="7,510,33,0"  >
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Description" Width="100">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True">
                                <TextBlock 
                                  Text="{Binding Description}" 
                                    HorizontalAlignment="Stretch"
                                    VerticalAlignment="Stretch"
                                     TextWrapping="Wrap"
                                    Height="10000"
                                    MinHeight="10000"
                                    MaxHeight="10000"
                                     />
                                </ScrollViewer>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
                
                
                <DataGrid.CommandBindings>
                        <CommandBinding Command="Copy" Executed="CopyCommand" />
                    </DataGrid.CommandBindings>

                    <!-- This is required to handle CTRL + C when something is selected in the DataGrid -->
                    <DataGrid.InputBindings>
                        <KeyBinding Key="C" Modifiers="Control" Command="Copy" />
                    </DataGrid.InputBindings>
                    <DataGrid.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Copy" Command="Copy" />
                        </ContextMenu>
                    </DataGrid.ContextMenu>
                </DataGrid>

Where I add the other columns in code behind currently

         grid.Columns.Add(new DataGridTextColumn() { Header = "QTY", Width = new DataGridLength(0.025, DataGridLengthUnitType.Star), Binding = new System.Windows.Data.Binding("Qty") });

            grid.Columns.Add(new DataGridTextColumn() { Header = "Description", Width = new DataGridLength(0.49, DataGridLengthUnitType.Star), Binding = new System.Windows.Data.Binding("Description") });
            grid.Columns.Add(new DataGridTextColumn() { Header = "Part Number", Width = new DataGridLength(0.1, DataGridLengthUnitType.Star), Binding = new System.Windows.Data.Binding("PartNo")  });
            grid.Columns.Add(new DataGridTextColumn() { Header = "Cost", Width = new DataGridLength(0.085, DataGridLengthUnitType.Star), Binding = new System.Windows.Data.Binding("Cost") { StringFormat = "$0.00" } });

Solution

  • The core issue is that you are setting the Height of the TextBlock, and not the ScrollViewer. So, in your case, the ScrollViewer is growing with the content, and therefore there is nothing to scoll.

    If you move your Height and/or MaxHeight poreties to the ScrollViewer, you should be able to achieve the scrolling affect you are looking for:

    <DataTemplate>
        <ScrollViewer VerticalScrollBarVisibility="Auto"
                      MaxHeight="100">
            <TextBlock Text="{Binding Description}"
                       HorizontalAlignment="Stretch"
                       VerticalAlignment="Stretch"
                       TextWrapping="Wrap"/>
        </ScrollViewer>
    </DataTemplate>
    

    Resulting scrollable cell: Resulting scrollable cell