2

I am using the standard DataGrid control in Silverlight 4. All data is readonly, i.e. no cell is editable.

Does anybody know how to display text in a cell such that it automatically wraps to the next line(s) (in the same cell) when the cell border is reached?

Any feedback is much appreciated.

Harald
  • 1,007
  • 1
  • 14
  • 27

2 Answers2

3

In the column definition set the TextWrapping property to "Wrap":

    <sdk:DataGridTextColumn 
        Header="Address"
        Width="150"
        Binding="{Binding Address}" >
        <sdk:DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
                <Setter Property="TextWrapping" Value="Wrap"/>
            </Style>
        </sdk:DataGridTextColumn.ElementStyle>
    </sdk:DataGridTextColumn>

Source

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • Thanks Chrisf for your quick response. Your suggestion works. Well, kinda. What happens is that the text wraps when i manually narrow the width of the column. However, the column height remains even when i widen the column again (such that no wrapping would be necessary). Any suggestions on that? – Harald Nov 29 '10 at 00:17
  • @hhwh - I don't know off hand. I'd have to experiment, but looking at the resize events would be the best place to start. – ChrisF Nov 29 '10 at 08:46
  • Yes, I have started doing that. It does get rather involved, tho. There is no specific column header resize event. I can capture it in LayoutUpdated event but it gets rather messy to extract the info I am looking for. Which makes me think that there should be a more simple way I am not (yet) getting. I am fairly new to Silverlight. I will keep trying and learning in the process but any pointers you or anybody else may have would be greatly appreciated. Cheers. – Harald Nov 29 '10 at 10:53
0

I don't think this is going to work. Try using a template column, put the TextBlock in there set HorizontalAlignment and VerticalAlignment to stretch and make sure to set the margins.

Brian
  • 6,910
  • 8
  • 44
  • 82
  • thanks for the suggestion, rhooligan. tried it but row heights remain at their highest, even when i change column width manually such that wrapping is not needed. – Harald Nov 29 '10 at 03:15