0
votes

We had some code successfully working with Xceed's WPF datagrid v.3.8. When we updated to their v.5.2 data grid, we lost one feature. All I want to do is get the grid column header captions to wrap to two lines. We set up a ContentTemplate on the column manager cells to do this, and it works on application startup. But, when you scroll the column headers off the screen and then back into view, the ContentTemplate formatting seems to be lost.

As I said, the same code works with the earlier version of the grid. Xceed will not help us with this problem because we are hosting the WPF grid on a Windows form (which was their suggestion in the first place).

The Xceed datagrid is on a .XAML UserControl. In the UserControl.Resources I define the DataTemplate like this:

<DataTemplate x:Key="columnManagerCellContentTemplate">
    <TextBlock Text="{Binding}" TextWrapping="WrapWithOverflow" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DataTemplate>

I define a Style for the ColumnManagerCell to set up a ColumnManagerCell loaded event handler:

<Style TargetType="{x:Type xcdg:ColumnManagerCell}">
     <EventSetter Event="Loaded" Handler="ColumnManagerCell_Loaded"/>
</Style>

And in the .XAML.cs file for the user control I have the ColumnManagerCell_Loaded event handler that assigns the ContentTemplate to the ColumnManagerCells:

internal void ColumnManagerCell_Loaded(object sender, RoutedEventArgs e)
{
    ColumnManagerCell columnManagerCell = sender as ColumnManagerCell;

    if (columnManagerCell != null)
    {
        if (columnManagerCell.ContentTemplate == null)
        {
            columnManagerCell.ContentTemplate = (DataTemplate)this.FindResource("columnManagerCellContentTemplate");
        }
    }
}

This is all code that was documented by Xceed a long time ago. As I said, it works to correctly wrap long column headers to 2 lines when I start the application. But, when I scroll the column headers out of view and then back into view, the formatting is gone.

I can also set the background color of the TextBlock in the DataTemplate, which works on startup, and then also disappears after scrolling the headers out of view.

Anyone have any suggestions on how I can get these settings to persist? I tried setting up an IsVisibleChanged event handler on the TextBlock and reset the TextWrapping property within that handler (I see that it is "None" at that point), but that didn't change the situation.

Any suggestions appreciated.

Thanks,

Mark

2

2 Answers

0
votes

You can override the default templates and set the ContentTemplate to reflect your requirements there instead. These are included as part of the Xceed install (under the themes folder).

If you locate the .xaml that contains the ColumnManagerCell template, then you can copy this and modify it as required. For example the snippet below is used to provide language translation capability to the column headers:

<StackPanel x:Name="contentPanel" Orientation="Horizontal">
  <!-- Same ContentPresenter as in the base Cell Template. -->
  <!--<xcdg:CellContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />-->
  <!--The following contentpresenter uses the language converter -->
  <ContentPresenter>
    <ContentPresenter.Content>
      <TextBlock Text="{TemplateBinding Content, Converter={StaticResource LanguageTranslationConverter}}"/>
    </ContentPresenter.Content>
  </ContentPresenter>    

  <ContentPresenter x:Name="sortGlyphPresenter"
                    Content="{x:Null}"
                    ContentTemplate="{x:Null}" />

  <ContentPresenter x:Name="sortIndexGlyphPresenter"
                    TextBlock.Foreground="{TemplateBinding Foreground}"
                    Content="{Binding Path=(xcdg:Cell.ParentCell).ParentColumn.SortIndex, RelativeSource={RelativeSource Self}, Converter={StaticResource IntAdditionConverter}, ConverterParameter=1}"
                    ContentTemplate="{x:Null}"
                    Margin="3,0,0,0"
                    Visibility="Collapsed" />                                        
</StackPanel> 

So you can modify this with the textwrapping and or backgrounds you need.

0
votes

The answer provided by Net Dev on the following post provides the right approach : Styling the Xceed ColumnManagerCell

Because of UI virtualization, properties should not be set directly on cells, but rather through styles. So for instance :

     <Style TargetType="{x:Type xcdg:ColumnManagerCell}">
        <Setter Property="Template">
           <Setter.Value>
              <ControlTemplate TargetType="ContentControl">
                    <TextBlock Text="{TemplateBinding ContentControl.Content}"
                               TextWrapping="Wrap"
                               HorizontalAlignment="Center"
                               VerticalAlignment="Center" />
              </ControlTemplate>
           </Setter.Value>
        </Setter>
     </Style>