Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Line break in datagrid header
4 replies. Latest Post by Amanda Wang - MSFT on July 2, 2009.
(0)
prl
Member
7 points
45 Posts
06-29-2009 6:33 AM |
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 nana...
Contributor
3388 points
624 Posts
06-29-2009 9:16 AM |
I have not tried but you can craft DataTemplate for your Header and put a TextBlock in it having
its TextWrapping "Wrap".
MarkMonster
5114 points
1,038 Posts
06-29-2009 9:30 AM |
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>
06-29-2009 9:54 AM |
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 W...
All-Star
17234 points
1,466 Posts
07-02-2009 6:34 AM |
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>