Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4099218
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:24:18+00:00 2026-05-20T20:24:18+00:00

I have a listbox and it’s bound to a list of simple objects. As

  • 0

I have a listbox and it’s bound to a list of simple objects. As we know the listbox by default has it’s items host as a Stackpanel and so it lays out items in this fashion

item1
item2
item3
item4
item5
item6

However i have some conditions through which i check whether the item is to be displayed horizontally or vertically and therefore the items could be laid out in this fashion

Eg :-

item1
item2
item3 item4 item 5
item6

How is it possible to do this?

(If you are wondering why would i ever need such a thing, an example scenario would be “facebook style updates” where if a user uploads 3-4 photos continously they don’t always appear on the next line but it may appear horizontally whereas if he posts some event it appears in next line.)

Thanks in advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:24:18+00:00Added an answer on May 20, 2026 at 8:24 pm

    The fundementals of the solution is to use another ItemsControl in the ItemTemplate of the ListBox. This ItemsControl should have a horizontal oriented StackPanel as its ItemsPanel.

    Here is a basic example. Lets start with some very simple testdata:-

    public class TestStringList : List<string>
    {
        public TestStringList()
        {
            AddRange(new[] {"Anthony", "Kar", "Martin", "Jon", "Erik", "Darin",
                "Balus", "Mike", "Hans", "Alex", "Anomie", "David" });
    
        }
    }
    

    Now we want to display this list in a ListBox but keep all the names that have the same first initial on the same line. I’m going to use an implementation of IValueConverter to deal with the grouping that we need. If you are using MVVM then you’d have your ViewModel take of this.

    public class Grouper : IValueConverter
    {
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((IEnumerable<string>)value).OrderBy(s => s).GroupBy(s => s[0]);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    The output of this converter is basically IEnumerable<IEnumerable<string>> which is what we want. The outer ListBox will enumerate the outer set and the inner ItemsControl will enumerate the inner set of strings which will be a set of names with the same initial.

    Here is the xaml:-

    <UserControl x:Class="SilverlightApplication1.SimpleGrouping"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SilverlightApplication1"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <UserControl.Resources>
            <local:TestStringList x:Key="TestData" />
            <local:Grouper x:Key="grouper" />
        </UserControl.Resources>
    
        <Grid x:Name="LayoutRoot">
            <ListBox ItemsSource="{Binding Converter={StaticResource grouper}, Source={StaticResource TestData}}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding}" Margin="5" />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </UserControl>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listbox bound to a list of business objects. The items in
I have a ListBox that each of its items has a button, I set
I have one ListBox which has three Items in it. And also has one
I have listbox that displays information about list of objects: <ListBox Grid.Column=0 Height=152 Name=CustomersList
I have a ListBox that has a bunch of items in it. However, I
I have ListBox called lstProductGroups. On a simple Windows Form, a method called GetGroups
I have a ListBox that has a style defined for ListBoxItems. Inside this style,
I have a listbox where the items contain checkboxes: <ListBox Style={StaticResource CheckBoxListStyle} Name=EditListBox> <ListBox.ItemTemplate>
I have a ListBox which displays items of variable height. I want to show
I have a listbox that is databound to a Collection of objects. The listbox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.