Remove Extra Space around Listbox

后端 未结 3 1268
野趣味
野趣味 2021-01-17 10:26

I have some extra space around my Listbox. It\'s 1px wide, but I don\'t know where it comes from...

\"The

相关标签:
3条回答
  • 2021-01-17 11:03

    This is the default control template for ListBox:

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

    Notice the Padding="1" on Border named Bd. Since this is harcoded and not template bound, you can either retemplate the ListBox and set the padding to 0, or since Padding on the ScollViewer has a TemplateBinding to the Padding of the ListBox, you can set the Padding on your ListBox to -1 to offset the padding on the border.

    0 讨论(0)
  • 2021-01-17 11:07

    The control template of a ListBox looks like this:

    <ControlTemplate TargetType="{x:Type ListBox}">
        <Border Name="Bd"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                SnapsToDevicePixels="true"
                Padding="1"> <!-- This might be the problem -->
        <!-- ... -->
    
    0 讨论(0)
  • 2021-01-17 11:10

    Try to put Padding="-1" in the ListBox.

    0 讨论(0)
提交回复
热议问题