Load only few items in ListView, but make all the items available to search in the CollectionView filter

Abhijith Harapanahalli 21 Reputation points
2021-04-22T16:50:08.28+00:00

I have a ListView that needs to display a ton of rows (think about 40,000+) and have a Filter implemented through a Textbox, where the user can search and find the item in the ListView. But I would like to display only a few rows (around 1000) in the listview , but make the entire 40,000+ rows searchable through the filter.
It doesn't make any sense to me to display all 40,000 rows which takes a lot of time to load and render in UI. I would rather show only a few rows to start with and have the user search for what they need.

Here's what I have for the code to implement ListView with Filter.

XAML:

<StackPanel Orientation="Horizontal" Width="Auto" Grid.Column="0">
    <StackPanel Orientation="Vertical" >
        <StackPanel Orientation="Horizontal" Margin="0">
            <Label Name="lblFilter" Content="Search"></Label>
            <TextBox Name="txtFilter" Text="{Binding LdapFilter,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="450">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="TextChanged">
                        <i:InvokeCommandAction Command="{Binding FilterTextChangedCommand}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </TextBox>
            <Button Content="Clear" Command="{Binding ClearCommand}" IsEnabled="{Binding CancelButtonEnabled}" FontSize="22" Margin="5" Foreground="White" Background="Maroon"></Button>
        </StackPanel>
        <ListView x:Name="LdapList" Height="Auto" SelectionMode="Multiple" Width="Auto"  Focusable="False" ScrollViewer.VerticalScrollBarVisibility="Auto"  VirtualizingPanel.IsVirtualizing="True"
                                              VirtualizingPanel.VirtualizationMode="Recycling" IsSynchronizedWithCurrentItem="True"
                                          ScrollViewer.CanContentScroll="True" ItemsSource="{Binding EmailListCollection}" >
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="BorderBrush" Value="#FFB8B8B8" />
                    <Setter Property="BorderThickness" Value="0,0,0,2" />
                    <Setter Property="Padding" Value="5,5,5,5"></Setter>
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Foreground" Value="#FFA7372B" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Email-ID" DisplayMemberBinding="{Binding EmailId,Mode=TwoWay}" Width="100"></GridViewColumn>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName,Mode=TwoWay}" Width="100"></GridViewColumn>
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName,Mode=TwoWay}" Width="100"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </StackPanel>
</StackPanel>

ViewModel:

public CollectionViewSource viewSource;

    public ObservableCollection<Person> emailList = new ObservableCollection<Person>();

    public ObservableCollection<Person> EmailList
    {
        get { return emailList; }
        set
        {
            if (value != emailList)
            {
                emailList = value;
                emailSelected = null;
                OnPropertyChanged("EmailLIst");
            }
        }
    }
    private string ldapFilter;

    public string LdapFilter
    {
        get { return ldapFilter; }
        set
        {
            if (value != ldapFilter)
            {
                ldapFilter = value;
                this.viewSource.View.Refresh();
                //FilterResults();
                OnPropertyChanged("LdapFilter");
            }
        }
    }

Model:

public class Person
{
    public int ID { get; set; }
    public string EmailId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,706 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
786 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,621 Reputation points
    2021-04-23T05:17:38.497+00:00

    I update your ViewModel to let the ListView show specified items when the query condition is null.

    public class ViewMode  
        {  
            public ViewModel()  
            {  
                maxShowItems = 20;  
      
                for (int i=0;i<40000;i++)  
                {  
                    EmailList.Add(new Person() { ID = i, FirstName = "Rose"+i.ToString(), LastName = "Bob", EmailId = "132165410@ss.com" });  
                }  
      
                PersonListView = new ListCollectionView(EmailList);  
                PersonListView.Filter += Filter;  
      
            }  
      
      
            private bool Filter(object obj)  
            {  
                Person person = obj as Person;  
                if (person == null) return false;  
      
                if (PersonListView.Count > maxShowItems && string.IsNullOrEmpty(ldapFilter))  
                {  
                    if (person.ID < maxShowItems)  
                    {  
                        return true;  
                    }  
                    return false;  
                }  
      
      
                if (string.IsNullOrEmpty(ldapFilter))  
                {  
                    return true;  
                }  
                else  
                {  
                    if (person.FirstName.Contains(ldapFilter))  
                    {  
                        return true;  
                    }  
                    return false;  
                }  
            }  
      
            public ListCollectionView PersonListView { get; set; }  
      
            public ObservableCollection<Person> emailList = new ObservableCollection<Person>();  
      
            public ObservableCollection<Person> EmailList  
            {  
                get { return emailList; }  
                set  
                {  
                    if (value != emailList)  
                    {  
                        emailList = value;  
                        OnPropertyChanged("EmailLIst");  
                    }  
                }  
            }  
      
      
            private string ldapFilter;  
            public string LdapFilter  
            {  
                get { return ldapFilter; }  
                set  
                {  
                    if (value != ldapFilter)  
                    {  
                        ldapFilter = value;  
                        this.PersonListView.Refresh();  
                        //FilterResults();  
                        OnPropertyChanged("LdapFilter");  
                    }  
                }  
            }  
      
      
            private int maxShowItems;  
            public int MaxShowItems  
            {  
                get { return maxShowItems; }  
                set  
                {  
                    if (value != maxShowItems)  
                    {  
                        maxShowItems = value;  
                        this.PersonListView.Refresh();  
                        OnPropertyChanged("MaxShowItems");  
                    }  
                }  
            }  
        }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Abhijith Harapanahalli 21 Reputation points
    2021-04-23T17:32:09.85+00:00

    Thank you @DaisyTian-MSFT . That worked

    0 comments No comments