.net 我的列表框中的项目之间的差距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4704009/
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
Gaps between items in my ListBox
提问by Rasto
When I create ListBoxwith horizontal items ordering for example like this:
当我创建ListBox水平项目时,例如这样的排序:
<DockPanel>
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>
<Button Content="Hello" />
</ListBoxItem>
<ListBoxItem>
<Button Content="Hello" />
</ListBoxItem>
</ListBox>
</DockPanel>
I have small gaps between buttons in the list as indicated by the arrows on following picture:
我在列表中的按钮之间有小间隙,如下图的箭头所示:


How can I get rid of those gaps please ? I need to have items in ListBoxjust next to each other. I have tried changing ItemTemplateof the ListBoxbut it did not help.
我怎样才能摆脱这些差距?我需要将物品放在ListBox一起。我试过改变ItemTemplate,ListBox但没有帮助。
回答by Jobi Joy
This is because of the padding inside the default ItemContainerStyle for ListBoxItem. To remove this you can override the ItemContainerStyle. For example just try the below Empty ItemContainerStyle to your ListBox and you can see the margin is no more.
这是因为 ListBoxItem 的默认 ItemContainerStyle 内的填充。要删除它,您可以覆盖 ItemContainerStyle。例如,只需将下面的 Empty ItemContainerStyle 尝试到您的 ListBox 中,您就可以看到边距不再存在。
<ListBox >
<ListBox.ItemsPanel>
<ItemsPanelTemplate >
<VirtualizingStackPanel IsItemsHost="True" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<Button Content="hello1" Width="75"/>
<Button Content="Hello2" Width="75"/>
</ListBox>
回答by H.B.
Those gaps are inside the ControlTemplateof the ListViewItems, you'd have to override that i'm afraid...
这些差距是里面ControlTemplate的ListViewItems,你必须重写恐怕...
Edit:On some platforms you do not even need to mess with the Templateto get rid of the gaps between items:
编辑:在某些平台上,您甚至不需要弄乱Template来消除项目之间的差距:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
</Style>
</ListBox.ItemContainerStyle>
To get rid of the gap at the very side you actually need to change the ListBox ControlTemplate itself, it's not a matter of the items. In the default Aero template there is a border with Padding = 1
要消除最侧面的差距,您实际上需要更改 ListBox ControlTemplate 本身,这不是项目的问题。在默认的 Aero 模板中有一个边框Padding = 1

