Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap text in Silverlight DataGridTextColumn

I am trying to create a simple DataGrid in Silverlight 4 Beta but cannot seem to figure out how to get my Note column to wordwrap.

The table represents notes made on an order, so they will be of variable length. i want the 'Note' cell to expand vertically to fit the contents.

I've found numerous similar questions but no answer. Is there something new in Silverlight 4 that will address this?

    <data:DataGrid AutoGenerateColumns="False" Name="dataGrid1" IsReadOnly="True">
        <data:DataGrid.Columns>
            <data:DataGridTextColumn Header="Date" Binding="{Binding Date}" />
            <data:DataGridTextColumn Header="User" Binding="{Binding User}" />
            <data:DataGridTextColumn Header="Note" Binding="{Binding Note}"  />
        </data:DataGrid.Columns>
    </data:DataGrid>
like image 614
Simon_Weaver Avatar asked Dec 11 '09 10:12

Simon_Weaver


2 Answers

Use a DataGridTemplateColumn instead of a text column. Have a TextBlock in there with it's TextWrapping set to true:

   <data:DataGrid x:Name="LayoutRoot" AutoGenerateColumns="False" IsReadOnly="True" RowDetailsVisibilityMode="Visible" >
        <data:DataGrid.Columns>

            <data:DataGridTextColumn Header="Date" Binding="{Binding Date}" />
            <data:DataGridTextColumn Header="User" Binding="{Binding User}" />

            <data:DataGridTemplateColumn Header="Note" Width="100">
                <data:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                            <TextBlock TextWrapping="Wrap" Text="{Binding Note}" />
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>
            </data:DataGridTemplateColumn> 

        </data:DataGrid.Columns>
    </data:DataGrid>
like image 163
James Hay Avatar answered Oct 19 '22 01:10

James Hay


I use ElementStyle to set the wrap text,and it works all right. Happy coding!

<Style x:Key="DataGridTextColumnStyle" TargetType="TextBlock">
    <Setter Property="TextWrapping" Value="Wrap"/>
</Style>

<data:DataGridTextColumn Binding="{Binding DESC}"   
         Header="Short Description" 
         HeaderStyle="{StaticResource DataGridHeaderStyle}"  
         ElementStyle="{StaticResource DataGridTextColumnStyle}"  >
</data:DataGridTextColumn>   
like image 27
HappyCoding Avatar answered Oct 19 '22 00:10

HappyCoding