列表框项目的水平方向

问题描述:

如何在列表框的默认样式中使列表框项目朝向水平方向.我的意思是默认情况下是我们使用blend的样式.

How to make the listbox items orientation to horizontal in the default styling of a listbox. What i mean by default is the style which we get using blend.

使用

Use the ItemsPanel property to replace the panel with a horizontal StackPanel:

<ListBox>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>


如果要在样式中执行此操作,只需添加用于设置ItemsPanel属性的Setter:


If you want to do this in a Style, just add a Setter that sets the ItemsPanel property:

<Style TargetType="ListBox">
    <!-- Rest of the style -->
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>