18

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.

Malcolm
  • 1,801
  • 3
  • 21
  • 48

1 Answers1

33

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

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

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>
Quartermeister
  • 57,579
  • 7
  • 124
  • 111
  • tx Quartermeister for the answer but i want to do this from style the default . I will just edit the style – Malcolm Aug 25 '10 at 12:09
  • hey Quartermeister tx once again but when done in style it throws me an error : Message: Unhandled Error in Silverlight Application Items collection must be empty before using ItemsSource. at System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value) – Malcolm Aug 25 '10 at 12:23
  • @Malcom: Its difficult to see how this could have broken it. If you leave ItemsPanelTemplate property alone entirely, leaving as the default vertical stackpanel, do you still get the error? – AnthonyWJones Aug 25 '10 at 12:29
  • oops sorry this error was for different issue..But it doesnt show up the data. His solution is rocking ..tx Quartermeister – Malcolm Aug 25 '10 at 13:16
  • I want to change the Orientation Horizontal to vertical dynamically based on page Orientation changed. Is it possible? – Arul Dinesh Nov 11 '13 at 04:35