如何从 WPF 中的 ListBox 中删除边框?

How to remove border from ListBox in WPF?

这是我的代码:

<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>

这是程序的图片:

如图所示,ListBox周围出现了一个框。我不明白为什么,因为我设置了 BorderThickness 和 Padding = "0"。

有人可以帮助我吗?

您已经设置了一个自定义的 ItemTemplate,它只应用于项目。

您还需要应用模板:

    <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>

并且在您的资源字典中:

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