I am working on an WPF MVVM application. I have a DataGrid, which is binded to a DataTable. AutoGenerateColumns is set to true.
However, I want to wrap the text of a column to 2 or 3 lines if needed. Currently, when I scroll the DataGrid, when the first column's text is longer, the first column expands as much as it needs and the last column is not seen anymore or is cut.
Here is the code:
<Grid.Resources>
<Style x:Key="ColumnHeaderStyle" TargetType="DataGridColumnHeader">
<Setter Property="ItemsControl.MinHeight" Value="50" />
<Setter Property="ItemsControl.MinWidth" Value="60" />
<Setter Property="ItemsControl.HorizontalContentAlignment" Value="Center" />
<Setter Property="ItemsControl.VerticalContentAlignment" Value="Center" />
</Style>
</Grid.Resources>
Grid:
<Grid
Width="800"
Height="500"
Visibility="Visible">
<Grid.Resources>
//code
</Grid.Resources>
<DataGrid
Name="Map"
Width="Auto"
Height="Auto"
Margin="0,10,0,0"
AutoGenerateColumns="True"
AutoGeneratingColumn="Map_AutoGeneratingColumn"
CanUserAddRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserResizeRows="True"
CanUserSortColumns="False"
ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}"
FontStretch="Normal"
IsReadOnly="True"
ItemsSource="{Binding MapDataTable}" />
I omitted things that are not relevant to the question.
Here is what I have tried:
Attempt 1:
<Grid.Resources>
<Style x:Key="ColumnHeaderStyle" TargetType="DataGridColumnHeader">
<Setter Property="ItemsControl.MinHeight" Value="50" />
<Setter Property="ItemsControl.MinWidth" Value="60" />
<Setter Property="ItemsControl.HorizontalContentAlignment" Value="Center" />
<Setter Property="ItemsControl.VerticalContentAlignment" Value="Center" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
Attempt 2:
<Grid.Resources>
<Style x:Key="ColumnHeaderStyle" TargetType="DataGridColumnHeader">
<Setter Property="ItemsControl.MinHeight" Value="50" />
<Setter Property="ItemsControl.MinWidth" Value="60" />
<Setter Property="ItemsControl.HorizontalContentAlignment" Value="Center" />
<Setter Property="ItemsControl.VerticalContentAlignment" Value="Center" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
</Grid.Resources>
Both of them don't work. What is the correct way to fix the text wrapping? AutoGenerateColumns MUST be true.