Question

I've had several problems with the GridViewColumnHeaders so far. Originally I had a issue with there being a tiny sliver of white between each of the column headers. Even if we set the borderthickness to 0, the white lines would still exist. After looking around, I found that I had to use a ControlTemplate to change the header to default to having textbox attributes. I used this code:

<Style x:Key="gridHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                        <TextBox Text="{TemplateBinding Content}" 
                                 FontWeight="Bold"
                                 FontFamily="Arial"
                                 FontSize="11"
                                 Foreground="#00648D"
                                 Padding="5,0,5,0" 
                                 BorderBrush="#7EB0CC" 
                                 BorderThickness="0,0,2,2"
                                 HorizontalContentAlignment="Center"
                                 VerticalContentAlignment="Center"
                                 IsReadOnly="True"
                                 Background="Transparent"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

This worked and removed the little sliver of white between the header columns and it also prevented the user from moving and resizing the column which messed up the formatting so that was good. However there is still a little sliver of white at the very end of the gridviewcolumnheader as shown in the image below:

enter image description here

Is there a way to remove that too?

Was it helpful?

Solution

That whitespace is the object you grab and resize the Header with. I believe it exists even if you re-write the header template because it is part of the GridView template, not the header column

I'm not sure if there's a way to overwrite those colors without overwriting the entire GridView template, however you can navigate the Visual Tree once it's loaded and manually set the background color from there.

Here's an example using the Loaded event of the ListView and some Visual Tree Helpers

private void ListView_Loaded(object sender, RoutedEventArgs e)
{
    var thumb = VisualTreeHelpers.FindChild<Thumb>((DependencyObject)sender, "PART_HeaderGripper");
    if (thumb == null) return;
    thumb.Background = Brushes.Transparent;

    var thumbContent = VisualTreeHelpers.FindChild<Border>(thumb);
    if (thumbContent == null) return;
    thumbContent.Background = Brushes.Transparent;
}

Result

Example

If you would rather to overwrite the ListView's ControlTemplate, you can find the default XAML here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top