Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get rid of a 1 pixel padding/border gap in a ListView?

Look at this!
Breaking!
I really need to get rid of this!
Look at the delcaration!

<ListView Name="list" BorderThickness="0">

How do I fix it?
This gap doesn't only happen to the scrollbar, it also happens to the items, with or WITHOUT any views.

like image 612
Vercas Avatar asked Feb 23 '23 07:02

Vercas


1 Answers

Looks like a problem in the Template, most likely a property of an internal control which is not bound to any of the properties the ListBox exposes to you.

Edit by Vercas: I have found the problem.
This is the template of the ListView:

<ControlTemplate x:Key="ListView" TargetType="ListBox">
    <Border Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="{TemplateBinding Control.BorderThickness}" Name="Bd" Padding="1" SnapsToDevicePixels="True">
        <ScrollViewer Focusable="False" Padding="{TemplateBinding Control.Padding}">
            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
        </ScrollViewer>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="UIElement.IsEnabled" Value="False">
            <Setter Property="Border.Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
        </Trigger>
        <Trigger Property="ItemsControl.IsGrouping" Value="True">
            <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Just change the Padding of the Border to 0 and you're done.
Here is the result if you don't want to bother finding the property.

<ControlTemplate x:Key="ListView" TargetType="ListBox">
    <Border Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="{TemplateBinding Control.BorderThickness}" Name="Bd" Padding="0" SnapsToDevicePixels="True">
        <ScrollViewer Focusable="False" Padding="{TemplateBinding Control.Padding}">
            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
        </ScrollViewer>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="UIElement.IsEnabled" Value="False">
            <Setter Property="Border.Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
        </Trigger>
        <Trigger Property="ItemsControl.IsGrouping" Value="True">
            <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Don't forget to add this template to your ListView!

like image 167
H.B. Avatar answered Mar 02 '23 23:03

H.B.