Search code examples
wpflistboxstylesborder

How to remove border from ListBox in WPF?


Here is my Code:

<Grid>
    <ScrollViewer Grid.Row="0" Grid.Column="1" HorizontalScrollBarVisibility="Disabled">
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                <TextBox MinWidth="80" Name="tbTodoName" Margin="5, 2"/>
                <Button Content="Add" Height="30" Margin="5, 0"/>
            </StackPanel>
            <ListBox Name="lstTodo" ItemsSource="{Binding}" BorderThickness="0" Padding="0" ItemTemplate="{StaticResource TodoTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Stretch">
            </ListBox>
        </StackPanel>
    </ScrollViewer>
</Grid>

and here is a picture of what the program looks like:

and here is a picture of what the program looks like

As can be seen in the picture, a frame is displayed around the ListBox. I don't understand why, since I set BorderThickness and Padding = "0".

Can someone help me?


Solution

  • You have set a custom ItemTemplate, which is applied only to the items.

    You will need to apply the Template as well:

        <Grid>
            <ScrollViewer Grid.Row="0" Grid.Column="1" HorizontalScrollBarVisibility="Disabled">
                <StackPanel Orientation="Vertical">
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                        <TextBox MinWidth="80" Name="tbTodoName" Margin="5, 2"/>
                        <Button Content="Add" Height="30" Margin="5, 0"/>
                    </StackPanel>
                    <ListBox Name="lstTodo" ItemsSource="{Binding}" BorderThickness="0" Padding="0" ItemTemplate="{StaticResource TodoTemplate}" Template={StaticResource ListBoxNoBorder} 
    ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Stretch">
                    </ListBox>
                </StackPanel>
            </ScrollViewer>
        </Grid>
    

    And in your resource dictionary:

    <ControlTemplate x:Key="ListBoxNoBorder" TargetType="{x:Type ListBox}">
        <Border BorderBrush="Transparent" BorderThickness="0">
            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
        </Border>
    </ControlTemplate>