Search code examples
c#wpfcomboboxwpf-controlscollectionview

Typing in Wpf ComboBox is behaving slow if the items are not in dropdown


I have combobox with items list of 53k with the underlying itemsource as a collectionview .It is an offline application using sqlite as the database. Combobox is populated with all the 53k items very effiently but the typing speed gets slow if i type anything which is not in the dropdown list .

                               <ComboBox IsEditable="True" HorizontalAlignment="Stretch" 
                                ItemsSource="{Binding Path=Items,Source={StaticResource PurchaseViewModel},Mode=TwoWay}"
                                DisplayMemberPath="[item_name]"
                                IsTextSearchCaseSensitive="False"
                                TextSearch.TextPath="[item_name]"
                                SelectedItem="{Binding Path=PurchaseItem,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource DataRowConverter}}"
                                IsSynchronizedWithCurrentItem="false">

                                <ComboBox.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <VirtualizingStackPanel />
                                    </ItemsPanelTemplate>
                                </ComboBox.ItemsPanel>

                            </ComboBox>

I have define the properties in ViewModel as such .

 public class PurchaseViewModel : INotifyPropertyChanged
    {
        
        private CollectionView _Items;

        public CollectionView Items
        {
            get
            {

                return _Items;
            }
            set
            {
                _Items = value;


                OnPropertyChanged("Items");
            }
        }


        public PurchaseViewModel()
        {

            Task.Run(() => GetItems());
         

        }


        public async Task GetItems()
        {

            DataView d = await ItemsService.GetAllItems(); 
            Items = CollectionViewSource.GetDefaultView(d) as CollectionView;

        }

        public event PropertyChangedEventHandler PropertyChanged;
        
        protected void OnPropertyChanged(string propertyName)
        {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
             
         }


   }

I have created a converter to convert the selecteditem of the combobox from datarowview to custom item object

 class DataRowConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is DataRowView && value != null)
            {
                DataRowView dataRowView = value as DataRowView;

                return dataRowView[1];
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          

            if (value is DataRowView && value != null)
            {
                DataRowView dataRowView = value as DataRowView;
                Item item = new Item();
                item.ItemId = int.Parse(dataRowView[0].ToString());
                item.Name = dataRowView[1].ToString();
                return item;
            }
            return value;
        }
    }

Everything works really fine but the problem is that if the search item is not in the list of the ComboxItems then the ComboxBox keyboard typing becomes very slow like if i press a single key, it is shown after a few seconds. Is there any WPF guru here who can save my job and suggest me a fix or an alternate solution to this problem of how to increase the combobox typing speed if the search term is not the list.i tried filtering the collectionview in a seperate thread but it gives error .Thanks


Solution

  • You have to use virtualized data approach. I used this article as example for my virualized collection. https://www.codeproject.com/Articles/34405/WPF-Data-Virtualization