Skip to main content

Microsoft Silverlight

Answered Question Line break in datagrid headerRSS Feed

(0)

prl
prl

Member

Member

7 points

45 Posts

Line break in datagrid header

Hello,

In my application I have a simple datagrid:

     <data:DataGrid x:Name="myDatagrid" Background="Transparent" AutoGenerateColumns="False">

            <data:DataGrid.Columns>
                <data:DataGridTextColumn Header="Header of my column"
                                         Width="130"
                                         IsReadOnly="true"
                                         Binding="{Binding Mode=OneWay, Path=Name}"
                                         SortMemberPath="Name" />
                
            </data:DataGrid.Columns>

        (...)

        </data:DataGrid>

 

I want to add line breaks in some of the headers because they're too big to be in one single row. I've been trying, but still wasn't able to do it. Is there any way?

Thanks in advance.

jay nanavati
jay nana...

Contributor

Contributor

3388 points

624 Posts

Re: Line break in datagrid header

I have not tried but you can craft DataTemplate for your Header and put a TextBlock in it having

its TextWrapping "Wrap".

Jay K Nanavaty
www.technologyopinion.com
Mark as answer if it helps. It will also help others...

MarkMonster
MarkMonster

Contributor

Contributor

5220 points

1,046 Posts

Re: Line break in datagrid header

With a textblock you can do things like this. Can you try that?

 

 
<TextBlock
  FontFamily="Arial" Width="400" Text="Sample text formatting runs">
  <LineBreak/>
  <Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>
  <LineBreak/>
  <Run Foreground="Teal" FontFamily="Times New Roman" FontSize="18" FontStyle="Italic">Times New Roman Italic 18</Run>
  <LineBreak/>
  <Run Foreground="SteelBlue" FontFamily="Verdana" FontSize="14" FontWeight="Bold">Verdana Bold 14</Run>
</TextBlock>
 

Mark Monster - MCPD Enterprise
http://mark.mymonster.nl
Silverlight and Expression Insiders UG

Dont forget to click "Mark as Answer" on the post that helped you.

prl
prl

Member

Member

7 points

45 Posts

Re: Line break in datagrid header

 Thanks for your replys. I managed to do it with this:

         <data:DataGridTemplateColumn Width="130"
                                             Header="My Header Text">
                    <data:DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="datap:DataGridColumnHeader">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="datap:DataGridColumnHeader">
                                        <TextBlock VerticalAlignment="Top"
                                                   TextAlignment="Left"
                                                   Text="{TemplateBinding Content}"
                                                   TextWrapping="Wrap" />
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </data:DataGridTemplateColumn.HeaderStyle>
                    <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </data:DataGridTemplateColumn.CellTemplate>
                </data:DataGridTemplateColumn>

 

(Got to admit it's too complicated for such a simple thing). But all the style in the header went away. How can I define the background color now?

Amanda Wang - MSFT
Amanda W...

All-Star

All-Star

17241 points

1,466 Posts

Answered Question

Re: Line break in datagrid header

 Hi,

prl:
(Got to admit it's too complicated for such a simple thing). But all the style in the header went away. How can I define the background color now?
 

You can try to place your textblock in a grid or a stackpanel, and  just set the grid or staclpanel's background, like below:

 <data:DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="datap:DataGridColumnHeader">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate>
                                            <StackPanel Background="Blue">
                                                    <TextBlock Text="{TemplateBinding  Content}"  />
                                             </StackPanel>
                                        </ControlTemplate>

                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </data:DataGridTemplateColumn.HeaderStyle>

Amanda Wang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities