Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Datagrid - Force singleline rows

I'm working on a datagrid in WPF and I have encountered a problem.

When adding a multiline string to my DataGridTextColumn, the row is expanded in height so as to fit the entire text. I wish the row height to remain constant at all times, ie only show the first line.

Does anyone know a solution? Seems like a simple enough task, but I havn't been able to find anything of worth on the subject.

Here is my XAML:

<DataGrid Grid.Row="0" AutoGenerateColumns="False" HorizontalAlignment="Stretch" Name="dgPosts" VerticalAlignment="Stretch"
                            SelectionMode="Single" SelectionUnit="FullRow" ItemsSource="{DynamicResource LogPosts}" CanUserAddRows="False"
                            IsReadOnly="True" GridLinesVisibility="Vertical" RowHeaderWidth="0" Margin="0,0,0,0"
                            CanUserReorderColumns="True" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="True" SelectionChanged="dgPosts_SelectionChanged">
   <DataGrid.Columns>
         <DataGridTextColumn Header="Tidpunkt" Width="120" Binding="{Binding Path=TimeStr}"/>
         <DataGridTextColumn Header="Posttyp" Width="55" Binding="{Binding Path=PostTypeStr}" CellStyle="{StaticResource CenterCellStyle}"/>
         <DataGridTextColumn Header="Beskrivning" Width="*" Binding="{Binding Path=Text}"/>
   </DataGrid.Columns>
   <DataGrid.RowStyle>
         <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="{Binding Path=Color}"/>
            <Style.Triggers>
               <Trigger Property="DataGridRow.IsSelected" Value="True" >
                     <Setter Property="Background" Value="Black" />
               </Trigger>
            </Style.Triggers>
         </Style>
   </DataGrid.RowStyle>
   <DataGrid.CellStyle>
         <Style TargetType="DataGridCell">
            <Setter Property="BorderThickness" Value="0"/>
         </Style>
   </DataGrid.CellStyle>
   <DataGrid.Resources>
         <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
   </DataGrid.Resources>
</DataGrid>

Thanks in advance!

like image 282
Fredrik Kuylenstierna Avatar asked May 24 '13 12:05

Fredrik Kuylenstierna


1 Answers

You just have to set a Row Height in the DataGrid:

<DataGrid RowHeight="50">
</DataGrid>

And that's it.

like image 172
Marc Avatar answered Nov 19 '22 17:11

Marc