Reputation: 9
I am new to this. My application has a two-column DataGrid control. Both contain text strings. The first column text always fits in its column but the text in the second column should wrap when it hits the column width or control width. Whenever it wraps, I would also like the content of column 1 to be centered vertically.
How can I get the second column to wrap and the first one's content to become centered vertically when wrapping occurs?
At present I am auto-generating the columns. I experimented with not auto-generating the columns and using the following:
<DataGrid x:Name="LogGrid" Margin="10,10,10,0" VerticalAlignment="Top" Loaded="LogGrid_Loaded" Background="#FFB8B5A4" MinWidth="380" HorizontalAlignment="Center" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" Width="380" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="TimeColumn" Header="Time" IsReadOnly="True" SortDirection="Ascending" Width="55"/>
<DataGridTextColumn x:Name="LogEntryColumn" Header="Log Entry" CanUserSort="False" Width="325">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
The result was that the text wrapping did not work and my data was not transferred into the control's columns (I am not using Data-Binding as I don't understand it yet). I couldn't figure out how to make the data appear in the manually defined columns. Will this force me to use Data Binding?
So I abandoned this approach and am awaiting help.
Thanks.
Upvotes: 0
Views: 413
Reputation: 400
Right click on "Edit Columns" on your DataGridView
, choose your column, expand "DefaultCellStyle" and set "WrapMode" = TRUE
Upvotes: 1