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 3966994
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:40:03+00:00 2026-05-20T03:40:03+00:00

I sort and group listbox items, I use CollectioView on this purpose. From view

  • 0

I sort and group listbox items, I use CollectioView on this purpose.

From view model class I bind collection on ListBox ItemSource property, here is it.

    public BindableCollection<UserInfo> Friends
    {
        get { return _friends; }
        set
        {
            _friends = value;
            NotifyOfPropertyChange(() => Friends);
        }
    }

ListBox items is type of UserInfo.

When I initialize ListBox I sort and group items with this method.

    private ICollectionView _currentView;

    //...

    private void SortContactList()
    {
        _currentView = CollectionViewSource.GetDefaultView(Friends);

        _currentView.GroupDescriptions.Add(new PropertyGroupDescription("TextStatus"));

        _currentView.SortDescriptions.Add(new SortDescription("TextStatus", ListSortDirection.Ascending));

        _currentView.SortDescriptions.Add(new SortDescription("Nick", ListSortDirection.Ascending));
    }

TextStatus and Nick are properties of userInfo class.

I use in Listbox GroupStyle. Here ist it:

    <Style x:Key="MessengerView_ToogleBtn" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Image x:Name="img" Source="/images/icons/Collapse.png" />
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="False">
                            <Setter TargetName="img" Property="Source" Value="/images/icons/Expand.png" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


   <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <ControlTemplate.Triggers>
                                        <DataTrigger Binding="{Binding Path=IsBottomLevel}" Value="True">
                                            <Setter TargetName="gridTemplate" Property="Grid.Background" Value="White" />
                                        </DataTrigger>
                                    </ControlTemplate.Triggers>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition />
                                            <RowDefinition />
                                        </Grid.RowDefinitions>
                                        <Grid Background="Black" 
                                                  x:Name="gridTemplate" 
                                                  Height="26" 
                                                  VerticalAlignment="Center">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto" />
                                                <ColumnDefinition Width="100" />
                                                <ColumnDefinition Width="45" />
                                            </Grid.ColumnDefinitions>

                                            <ToggleButton x:Name="btnShowHide" 
                                                          IsChecked="True" 
                                                          Style="{StaticResource MessengerView_ToogleBtn}"/>

                                            <TextBlock Style="{StaticResource MessengerView_LbGroupHeader_TextBlock}"  
                                                       Text="{Binding Path=Name}" 
                                                       Grid.Column="1"/>
                                            <TextBlock TextAlignment="Left" Style="{StaticResource MessengerView_LbGroupHeader_TextBlock}" 
                                                       Grid.Column="2" 
                                                       Text="{Binding Path=ItemCount}"/>

                                        </Grid>

                                        <ItemsPresenter Visibility="{Binding ElementName=btnShowHide, Path=IsChecked,
                                                                            Converter={StaticResource booleanToVisibilityConverter}}"
                                                            Margin="3,3,3,3"
                                                            Grid.Row="1"  />

                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>

If I run app, it look on this picture.

1.
enter image description here

I edit source property of ListBox, (add,remove, update), after edited listbox I call Refresh method on CollectionView.

            _currentView.Refresh();

Problem is that GroupItem is collapse and I call Refresh method on all GroupItem are expanded.

For example.

GroupItem 1 is collapse.

GroupItem 2 is exapnded.

GroupItem 3 is collapse.

Before call Refresh ListBox look like on this picture:

enter image description here

I call Refresh method on CollectionView and all GroupItems are expanded. I would like to keep the original state, how can I achive this?

After called Refresh Lisbox look like on first picture on the top.

  • 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-20T03:40:04+00:00Added an answer on May 20, 2026 at 3:40 am

    The workaround isn’t perfect. As I said, it is better to use ViewModels with your own grouping, but it will require much more code.

    You need two event handlers:

        private Dictionary<string, bool?> expandStates = new Dictionary<string, bool?>();
    
        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            var grid = (Grid)sender;
            var dc = grid.DataContext as CollectionViewGroup;
            var groupName = (string)dc.Name;
    
            //If the dictionary contains the current group, retrieve a saved state of the group
            if (this.expandStates.ContainsKey(groupName))
            {
                var btn = (ToggleButton)grid.FindName("btnShowHide");
                btn.IsChecked = this.expandStates[groupName];
            } //Else add default state
            else this.expandStates.Add(groupName, true);
    
        }
    
        private void btnShowHide_Click(object sender, RoutedEventArgs e)
        {
            var btn = (ToggleButton)sender;
            var dc = (CollectionViewGroup)btn.DataContext;
            var groupName = (string)dc.Name;
    
            //Loaded event is fired earlier than the Click event, so I'm sure that the dictionary contains the key
            this.expandStates[groupName] = btn.IsChecked; //Save the current state
        }
    

    They are bound with controls here:

    <ControlTemplate TargetType="{x:Type GroupItem}">
        <Grid Loaded="Grid_Loaded">
    

    and here:

    <ToggleButton x:Name="btnShowHide" Click="btnShowHide_Click" IsChecked="True" Margin="3.5" />
    

    If you define Template for GroupItem somewhere in an external dictionary, you must use UserControl for the purpose of having access to code-behind.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I bind collection from Caliburn Micro on ListBox control in view. Here is
I'd sort of like to use SQLite from within C#.Net, but I can't seem
function safe(){ if($(this).is(':checked')){ $(select[name='sort']).attr(disabled, disabled); $(input[name='group']).attr(disabled, disabled) } else { $(select[name='sort']).attr(disabled, false); $(input[name='group']).attr(disabled, false)
This is sort of SQL newbie question, I think, but here goes. I have
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
This is the sort of thing I want to be able to do. I
I have a MySQL database that looks similar to this: ID Group Name 1
I have a list of customer and i need to sort and group the
I would like to sort an array in ascending order using C/C++ . The
I often have to sort a dictionary (consisting of keys & values) by value.

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.