DataGrid Wrap Column Header and Header Row Height

  • Zuhaib 1 year, 8 months ago

    Hi,

    How do I wrap a DataGrid Column header?

    I have used the following templates and they don’t work.

    <Style TargetType="c1:DataGridColumnHeaderPresenter">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" TextWrapping="Wrap" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
    OR
    
    <Style TargetType="c1:DataGridColumnHeaderPresenter">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock Text="{TemplateBinding Header}" TextWrapping="Wrap" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    The first one doesn’t display the Header text and the second one throws error.

    In the first example when the header is wrapped the Header Row Height doesn’t change automatically.

    Let me know how can I set the Column Headers to wrap and set the Header Row Height to Auto.

  • 22p
    C1_Deepankar22p 1 year, 8 months ago

    Hi,

    You can set the following code to set the TextWrapping on the Column headers.

    <c1grid:DataGridTextColumn Width="100" Binding="{Binding Name, Mode=TwoWay}">
        <c1grid:DataGridTextColumn.Header>
               <TextBlock TextWrapping="Wrap" Text="Long Column Name Text"></TextBlock>
         </c1grid:DataGridTextColumn.Header>
    </c1grid:DataGridTextColumn>
    

    As of now, auto sizing the Header Row height is not possible. You will have to manually set the Row Height.

    void c1DataGrid1_LoadedColumnHeaderPresenter(object sender, C1.Silverlight.DataGrid.DataGridColumnEventArgs e)
    {
         if (e.Column.HeaderPresenter != null)
         {
             TextBlock tb = e.Column.HeaderPresenter.Content as TextBlock;
             tb.Loaded += new RoutedEventHandler(tb_Loaded);
          }
    }
    
    void tb_Loaded(object sender, RoutedEventArgs e)
    {            
         TextBlock tb = (sender as TextBlock);
         var height = tb.ActualHeight;
         if (height > c1DataGrid1.ColumnHeaderHeight)
             c1DataGrid1.ColumnHeaderHeight = height+5;
    } 
    

    Hope this will help. Let me know if any further assistance is required.

    Regards,

  • Zuhaib 1 year, 8 months ago

    Deepankar,

    The above code would only work for the first time. If the column width isn’t set and the column is resized the Header Height wont change.

    I need the Row height to resize Automatically. Any idea if this will be fixed in the new release?

    Also I don’t want to define TextBlock definition for each and every column header. I wan’t to do that using a Style.

    What should be the Binding of Text property of the TextBlock for DataGridColumnHeaderPresenter’s ContentTemplate?

    <Style TargetType="c1:DataGridColumnHeaderPresenter">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" TextWrapping="Wrap" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
  • Zuhaib 1 year, 8 months ago

    Subscribing to the SizeChanged will work, but what is the correct Binding for the TextBlock Text property inside a Style?

    I don’t want to modify each and every column in all my pages. There should be a way of using Style and then subscribe to the SizeChanged event of the DataGrid.

  • 22p
    C1_Deepankar22p 1 year, 8 months ago

    Hi,

    I am not sure if XAML binding is really possible for HeaderStyle Template. DataContext is coming out as Null and that is the reason we do not see the Text. I would be looking into this further and get back to you with the observations.

    Regards,

  • Zuhaib 1 year, 8 months ago

    I want to create a Style that displays the value of the Column Header Property in a TextBlock and sets the Text Wraping properties.

    I don’t think DataContext is a problem there. Something is wrong with my Template Binding or probably it needs to be implemented in a different way.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.