How to filter listbox in WPF

Usha Patil 31 Reputation points
2020-12-01T07:28:01.223+00:00

How to filter listbox in WPF

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,769 questions
0 comments No comments
{count} vote

Accepted answer
  1. Rajanikant Hawaldar 91 Reputation points
    2020-12-01T10:03:37.407+00:00

    Refer below link for filter using ICollectionView and CollectionViewSource.
    Auto-complete-quick-search-WPF-ListBox

    Below very sample filtering, added data in XAML code only, you can bind listbox as per your need.
    C# Code:

    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    namespace WpfListboxSearch
    {
        public partial class MainWindow : Window
        {
            List<string> Namelist = new List<string>();
            public MainWindow()
            {
                InitializeComponent();
    
                foreach (ListBoxItem lbi in lbNameList.Items)
                {
                    Namelist.Add(lbi.Content.ToString());
                }
            }
            private void txtNameSearch_TextChanged(object sender, TextChangedEventArgs e)
            {
                if (!string.IsNullOrWhiteSpace(txtNameSearch.Text))
                {
                    lbNameList.Items.Clear();
                    foreach (string name in Namelist)
                    {
                        if (name.ToLower().StartsWith(txtNameSearch.Text.Trim().ToLower()))
                        {
                            lbNameList.Items.Add(name);
                        }
                    }
                }
                else if (string.IsNullOrWhiteSpace(txtNameSearch.Text))
                {
                    lbNameList.Items.Clear();
    
                    foreach (string name in Namelist)
                    {
                        lbNameList.Items.Add(name);
                    }
                }
            }
        }
    }
    

    XAML Code:

    <Window x:Class="WpfListboxSearch.MainWindow"
            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:WpfListboxSearch"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="82,11,0,0" Name="txtNameSearch" VerticalAlignment="Top" Width="120" TextChanged="txtNameSearch_TextChanged"/>
            <ListBox Height="100" HorizontalAlignment="Left" Margin="86,51,0,0" Name="lbNameList" VerticalAlignment="Top" Width="600" >
                <ListBoxItem>David</ListBoxItem>
                <ListBoxItem>Rock</ListBoxItem>
                <ListBoxItem>Shaan</ListBoxItem>
            </ListBox>
        </Grid>
    </Window>
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.