1

I have three different List collections.

How can I show them in one ListBox in three groups?

List<string> pending;
List<string> busy;
List<string> completed;

The end result should be something like this: vbcity.com/cfs-file.ashx/__key/…. But the only examples I find are working with one list.

Tom
  • 1,330
  • 11
  • 21
TWT
  • 2,511
  • 1
  • 23
  • 37

3 Answers3

3

Create a listbox with a Listview for the data template in your xaml code:

<Grid>
    <ListBox Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="lbxTasks"    VerticalAlignment="Top" Width="120">
        <ListBox.GroupStyle>
            <GroupStyle />
        </ListBox.GroupStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ListView ItemsSource="{Binding Queue}" BorderThickness="0"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

Then for your code behind, create and bind the lists. This is an example to test with:

namespace WpfApplication3
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        List<string> pending = new List<string> {"1","2"};
        List<string> busy = new List<string> { "4", "5" };
        List<string> completed = new List<string> { "7", "8" };

        private List<Tasks> MyTasks()
        {
            List<Tasks> tasks = new List<Tasks>();
            tasks.Add(new Tasks {Status = "Pending", Queue = pending});
            tasks.Add(new Tasks {Status = "Busy",Queue = busy});
            tasks.Add(new Tasks {Status = "Completed", Queue = completed});
            return tasks;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ICollectionView _tasksView = CollectionViewSource.GetDefaultView(MyTasks());
            _tasksView.GroupDescriptions.Add(new PropertyGroupDescription("Status"));
            lbxTasks.ItemsSource = _tasksView;
        }
    }

    public class Tasks
    {
        public List<string> Queue { get; set; }
        public string Status { get; set; }
    }
}

This creates a listbox for each Queue (or List) that you provide and groups it by the name of the List.

The output:

Output of the application

SOURCE: Modified the example from the link below into this format. LISTBOX grouping using collectionView Source

Community
  • 1
  • 1
Tom
  • 1,330
  • 11
  • 21
2

you can group them easily with a ListView.

How to: Group Items in a Windows Forms ListView Control

Blau
  • 5,742
  • 1
  • 18
  • 27
  • The original question did not reference WPF. – Dave Ziegler Jun 26 '12 at 19:31
  • 1
    I did not realize that was about WPF, I'm not good with wpf, but you have listview in wpf too... You can get info about that question here http://stackoverflow.com/questions/639809/how-do-i-group-items-in-a-wpf-listview – Blau Jun 26 '12 at 19:56
  • @Blau - great link. If you are interested, see my answer below for WPF. – Tom Jun 26 '12 at 20:09
  • Wpf seems powerful, but I continue seeing a big wall in front of me... :( – Blau Jun 26 '12 at 22:01
  • Once you make the jump, you won't regret it. :) – Tom Jun 27 '12 at 00:46
0

You could add them individually and when you are done the first group, add an item with a null or empty string value and "------" as the display value, which will indicate separation to the user.

Mark Sherretta
  • 10,160
  • 4
  • 37
  • 42
  • What i actualy want is something like this: http://vbcity.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/xtab.metablogapi/5008.ListBoxGrouping_5F00_429ADC50.png. But the only examples I find are working with one list. – TWT Jun 26 '12 at 19:27