从WPF ListBox中删除所有左边距

use*_*309 27 c# wpf

WPF ListBox控件中的每个项似乎都有一些左边填充.如何重新移除所有填充?

默认比较

Ken*_*art 45

您还可以按如下方式设置ListBoxItems的样式ListBox:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Padding" Value="0"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
Run Code Online (Sandbox Code Playgroud)


小智 5

我不太确定为什么会发生这种情况(它似乎不是ListBox而是ListBoxItem的问题.如果你将LBI的Background属性设置为红色或其他颜色你可以看到它是向左冲洗LB的一面.

快速破解是为按钮设置负余量: Margin =" - 3 0 0 0" 这可能会导致一些意想不到的副作用,但在视觉上有效...

编辑 快速检查确认LBI具有默认填充.你可以这样关掉它:

<ListBoxItem Padding="0"><!-- content here k --></ListBoxItem>
Run Code Online (Sandbox Code Playgroud)

或者,你可以在你的Window的资源中拍一个样式,这将从你项目中的所有LBI中删除它(这可能是pseudoxaml,但你明白了):

<Style TargetType="ListBoxItem">
  <Setter Property="Padding" Value="0"/>
</Style>
Run Code Online (Sandbox Code Playgroud)