DataGridRowHeader ControlTemplate

Dec 8, 2008 at 8:51 PM
Hi,

I am having great trouble applying a new ControlTemplate to the DataGridRowHeader. As a first step I have copied the template from the toolkit and applied it in a Style as follows, but the RowHeader just appears blank (white). I have used the same approach with the ColumnHeader successfully.

  <Style x:Key="GridRowHeaderStyle" TargetType="{x:Type toolkit:DataGridRowHeader}">
   <Setter Property="Background" Value="Red"/>
   <Setter Property="BorderBrush" Value="Green"/>
   <Setter Property="SeparatorBrush" Value="Blue"/>
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="toolkit:DataGridRowHeader">
      <Grid>
       <toolkit:DataGridHeaderBorder IsSelected="{TemplateBinding IsRowSelected}"
                                     IsHovered ="{TemplateBinding IsMouseOver}"
                                     IsPressed="{TemplateBinding IsPressed}"
                                     Background="{TemplateBinding Background}"
                                     BorderBrush="{TemplateBinding BorderBrush}"
                                     BorderThickness="{TemplateBinding BorderThickness}"
                                     Padding ="{TemplateBinding Padding}"
                                     Orientation="Horizontal"
                                     SeparatorVisibility="{TemplateBinding SeparatorVisibility}"
                                     SeparatorBrush="{TemplateBinding SeparatorBrush}">

        <StackPanel Orientation="Horizontal">
         <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
         <Control SnapsToDevicePixels="false"
                           Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type toolkit:DataGridRow}}, Path=(Validation.HasError), Converter={StaticResource bool2VisibilityConverter}}"
                           Template="{Binding RelativeSource={RelativeSource AncestorType={x:Type toolkit:DataGridRow}}, Path=ValidationErrorTemplate}" />
        </StackPanel>
       </toolkit:DataGridHeaderBorder>
       <Thumb x:Name="PART_TopHeaderGripper"
                   VerticalAlignment="Top"
                   Style="{StaticResource RowHeaderGripperStyle}"/>
       <Thumb x:Name="PART_BottomHeaderGripper"
                   VerticalAlignment="Bottom"
                   Style="{StaticResource RowHeaderGripperStyle}"/>
      </Grid>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>

Please can someone tell me what is wrong here?

Thanks

Coordinator
Dec 9, 2008 at 2:28 AM
Edited Dec 9, 2008 at 2:29 AM
Hi,

This seems to be bug with DataGridRowHeader implementation. I have two workarounds for you....

  • Use implicit style instead of a keyed style and this shouldnt have any problems. The best way to do this would be to retain the above key style and not use it with DataGrid.RowHeaderStyle property but use it as follows instead....

        <dg:DataGrid ...>
            <dg:DataGrid.Resources>
                <Style TargetType="{x:Type dg:DataGridRowHeader}" BasedOn="{StaticResource GridRowHeaderStyle}"></Style>
            </dg:DataGrid.Resources>
        </dg:DataGrid>

  • Second approach would be to put an event setter in the style as follows

             <Style x:Key="GridRowHeaderStyle" TargetType="{x:Type toolkit:DataGridRowHeader}">
                <EventSetter Event="Loaded" Handler="DataGrid_RowHeader_Loaded"/>
                ... <!-- your remaining style -->
            </Style>

        private void DataGrid_RowHeader_Loaded(object sender, RoutedEventArgs e)
        {
            DataGridRowHeader header = sender as DataGridRowHeader;
            if (header != null)
            {
                header.CoerceValue(StyleProperty);
            }
        }

Either of these workarounds should work for you.

Dec 10, 2008 at 8:06 PM
Thanks, I've tried the first approach and that works OK for me.