在我的列表框项目之间的差距我的、差距、项目、列表

2023-09-03 15:46:54 作者:傲视之巅

当我创建列表框与水平的项目排序,例如是这样的:

When I create ListBox with horizontal items ordering for example like this:

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

我怎样才能摆脱这些差距的吗?我需要有项目在列表框只是彼此相邻。我试图改变 ItemTemplate中列表框,但它并没有帮助。

How can I get rid of those gaps please ? I need to have items in ListBox just next to each other. I have tried changing ItemTemplate of the ListBox but it did not help.

推荐答案

这是因为默认ItemContainerStyle的ListBoxItem的内部填充。要删除这一点,你可以覆盖ItemContainerStyle。例如只是尝试下面的空ItemContainerStyle你的列表框,你可以看到的保证金是没有更多的。

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.

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