Gaps between items in my ListBox

后端 未结 2 1788
北恋
北恋 2020-12-30 20:13

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


    
        

        
相关标签:
2条回答
  • 2020-12-30 20:35

    Those gaps are inside the ControlTemplate of the ListViewItems, you'd have to override that i'm afraid...

    Edit: On some platforms you do not even need to mess with the Template to get rid of the gaps between items:

        <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

    0 讨论(0)
  • 2020-12-30 20:48

    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>
    
    0 讨论(0)
提交回复
热议问题