Answered Problem with grouping items c#

  • Tuesday, May 08, 2012 10:46 AM
     
     

    How can I gruop items in a listbox  according to size in brackets.... example to group according to name we use  GroupBy(name=>name)

    how to about size  in bracket   ex    

    AAAA[3]

    BBBB[2]

    CCCC[5]

    and then to goup like that
    CCCC[5]

    AAAA[3]

    BBBB[2]

    thanx

All Replies

  • Thursday, May 10, 2012 8:21 AM
    Moderator
     
     Answered Has Code

    If you can use the GoupBy() in C# codem that is one C#/Linq question, not a WPF.

    If you want to know how to group it and show in the ListBox that can show the items in specific groups. That is one WPF question, and you could view below articles about the grouping in WPF:

    http://karlshifflett.wordpress.com/2009/06/10/wpf-sample-series-listbox-grouping-sorting-subtotals-and-collapsible-regions/

    And the sample:

    <Window x:Class="WpfApplication9.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
      <Grid>
        <Grid.Resources>
          <CollectionViewSource x:Key="source" Source="{Binding}">
            <CollectionViewSource.GroupDescriptions>
              <PropertyGroupDescription PropertyName="Size"/>
            </CollectionViewSource.GroupDescriptions>
          </CollectionViewSource>
        </Grid.Resources>
        <ListBox ItemsSource="{Binding Source={StaticResource source}}">
          <ListBox.GroupStyle>
            <GroupStyle>
              <GroupStyle.HeaderTemplate>
                <DataTemplate>
                  <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
              </GroupStyle.HeaderTemplate>
            </GroupStyle>
          </ListBox.GroupStyle>
          <ListBox.ItemTemplate>
            <DataTemplate>
              <TextBlock>
                <TextBlock.Inlines>
                  <Run Text="{Binding Name}"/>
                  <Run Text="("/>
                  <Run Text="{Binding Size}"/>
                  <Run Text=")"/>
                </TextBlock.Inlines>
              </TextBlock>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
      </Grid>
    </Window>
    

    C#:

      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
          this.DataContext = new ObservableCollection<Item>()
          {
            new Item(){Name="AAAA", Size=3},
            new Item(){Name="BBBB", Size=2},
            new Item(){Name="CCCC", Size=3},
            new Item(){Name="DDDD", Size=5},
            new Item(){Name="EEEE", Size=3},
            new Item(){Name="FFFF", Size=4},
            new Item(){Name="GGGG", Size=2},
            new Item(){Name="XXXX", Size=5},
            new Item(){Name="VVVV", Size=4}
          };
        }
      }
    
      public class Item
      {
        public string Name { get; set; }
        public int Size { get; set; }
      }


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us