Skip to main content

Microsoft Silverlight

Answered Question Word wrap in a datagridRSS Feed

(0)

tcook815
tcook815

Member

Member

1 points

14 Posts

Word wrap in a datagrid

I have a Silverlight datagrid that completely works and everything.  My only question I never could find an answer for.  I just want the cells to have word wrap.  I created my columns like this

<data:DataGridTextColumn Header="Text of Message" Width="Auto" MinWidth="200" MaxWidth="400" Binding="{Binding Message}" IsReadOnly="False"/>

 The text inside of the cells is longer than 400, so it dissappears.  Is there a way to get the word wrap?  Thank you anyone who can help.

gunjanshah21
gunjansh...

Member

Member

526 points

88 Posts

Re: Word wrap in a datagrid

DataGridTextColumn currently doesn't support TextWrapping. You could use a TextBlock in a DataGridTemplateColumn instead to achieve this:

<data:DataGridTemplateColumn MinWidth="200" MaxWidth="400" Header="MyLongStringTemplateColumn">
                    <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock TextWrapping="Wrap" Text="{Binding LongString}"/>
                        </DataTemplate>
                    </data:DataGridTemplateColumn.CellTemplate>
                </data:DataGridTemplateColumn>

 Gunjan [MSFT]

tcook815
tcook815

Member

Member

1 points

14 Posts

Re: Word wrap in a datagrid

Hmm... so I have to go in and turn everything into text boxes... welp, I have a lot of datagrids I have to go and do this to, but if this is the only way to get wrapping, then alright. Thank you for your help!

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft
Answered Question

Re: Word wrap in a datagrid

You don't need to convert everything to TextBlocks.  You can get to the TextWrapping property by setting an ElementStyle on the column.  There's also EditingElementStyle if you need to do something similar with the editing element.

 

        <data:DataGrid x:Name="dataGrid" AutoGenerateColumns="False">
            <data:DataGrid.Columns>
                <data:DataGridTextColumn Binding="{Binding Name}">
                    <data:DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextWrapping" Value="Wrap" />
                        </Style>
                    </data:DataGridTextColumn.ElementStyle>
                </data:DataGridTextColumn>
            </data:DataGrid.Columns>
        </data:DataGrid>
 

Yifung Lin [MSFT]

tcook815
tcook815

Member

Member

1 points

14 Posts

Re: Word wrap in a datagrid

This works out how I want it to.  Thank you everyone for your help!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities