C# 从 WPF ListBox 中删除所有左填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/435925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:19:25  来源:igfitidea点击:

Remove all left padding from WPF ListBox

c#wpf

提问by user38309

Each item in a WPF ListBox control seems to have a bit of left padding. How can I restyle to remove all padding?

WPF ListBox 控件中的每个项目似乎都有一点左填充。如何重新设置样式以删除所有填充?

Default Comparison

默认比较

采纳答案by Kent Boogaart

You can also style the ListBoxItems for a specific ListBoxas follows:

您还可以为ListBoxItem特定设置s样式ListBox,如下所示:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Padding" Value="0"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

回答by user38309

I'm not quite sure WHY this is happening (it appears to be an issue NOT with the ListBox but the ListBoxItem. If you set the Background property of the LBI to red or some other color you can see that it is flush to the left side of the LB.

我不太确定为什么会发生这种情况(这似乎不是 ListBox 而是 ListBoxItem 的问题。如果您将 LBI 的背景属性设置为红色或其他颜色,您可以看到它与左侧齐平LB的一侧。

A quick hack is to set a negative margin for the button: Margin="-3 0 0 0"That might cause some unintended side effects but works visually...

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

editA quick check confirmed that the LBI has default padding. You can turn it off like this:

编辑快速检查确认 LBI 具有默认填充。您可以像这样关闭它:

<ListBoxItem Padding="0"><!-- content here k --></ListBoxItem>

Alternately, you can slap a style in your Window's resources that will remove this from all LBIs in your project (this might be pseudoxaml, but you get the idea):

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

<Style TargetType="ListBoxItem">
  <Setter Property="Padding" Value="0"/>
</Style>