0

Basically what I have is a wpf combo box with both isTextSearchEnable and isEditable, enabled. I want to allow use to key in what they want to find and filter the combo box items to show relatable items only.

If the user keys in MP-1, it should only show items that contains with those text and not show unrelated items. So far, the code works for the 2nd character but not the first. Any ideas or what am I doing wrong here? Entering 1st character Entering 2nd character

The code that I have so far,

public class FilterViewModel
        {
            public IEnumerable<string> DataSource { get; set; }

            public FilterViewModel()
            {
                DataSource = new[] { "MP-10001", "MP-10002", "MP-10003", "MP-10004", "XD-20001", "XD-20002", "XD-20003" };
            }
        }

        public MainWindow()
        {
            InitializeComponent();

            FilterViewModel vm = new FilterViewModel();
            this.DataContext = vm;
        }


        private void FilteredCmb_KeyUp(object sender, KeyEventArgs e)
        {
            CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(cmbBox.ItemsSource);
            
            cv.Filter = ((o) =>
            {
                if (String.IsNullOrEmpty(cmbBox.Text)) return true;
                else
                {
                    if (((string)o).Contains(cmbBox.Text)) return true;
                    else return false;
                }
            });

            cv.Refresh();            
        }
<Grid>
        <ComboBox x:Name="cmbBox" HorizontalAlignment="Left" Margin="78,36,0,0" VerticalAlignment="Top" Width="362" IsEditable="True" IsTextSearchEnabled="True" Height="47" KeyUp="FilteredCmb_KeyUp" StaysOpenOnEdit="True" IsDropDownOpen="True" ItemsSource="{Binding DataSource}"/>

    </Grid>

2 Answers2

0

The default behaviour of isTextSearchEnable="True" is selecting the first partial match and so your filter matches the entire item and returns false for all other entries. Take a look at the xaml on the question you seem to have gotten that code from: Simple WPF combobox filter

T.Schwarz
  • 130
  • 6
  • Why is it that I search "P" It returns multiple results? – TheLazyUnicorn May 31 '21 at 15:11
  • because in that case your Filter worked like intended. The strings contained the letter P but the built in TextSearch didn't find a match as none of the strings start with P – T.Schwarz Jun 01 '21 at 07:08
0

You can use AutoCompleteBox, nice control that contains advanced features of seaching and filtering, install this Package:

PM> Install-Package WPFToolkit

Here is a nice tutorial about it : https://www.broculos.net/2014/04/wpf-autocompletebox-autocomplete-text.html

for the download link it removed from codeplex, so you can find it here : https://github.com/dotnetprojects/WpfToolkit

Or xceed have a great control :

Install-Package Extended.Wpf.Toolkit
Ahmed HM
  • 110
  • 1
  • 9
  • Is there any example using this package that I can refer to? – TheLazyUnicorn May 31 '21 at 15:04
  • Here is a complete tutorial about it : https://www.broculos.net/2014/04/wpf-autocompletebox-autocomplete-text.html – Ahmed HM May 31 '21 at 15:06
  • I tried installing the package via the console, but couldn't. nstall-Package : Unable to find package 'WPFToolkit' At line:1 char:1 + Install-Package WPFToolkit + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Install-Package], Exception + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand Any ideas why I'm getting this error? – TheLazyUnicorn May 31 '21 at 16:00
  • Yes I did, the first step is to install the package, I search via package manager couldn't find it, via console also couldn't – TheLazyUnicorn May 31 '21 at 16:10
  • Please check this link : https://github.com/dotnetprojects/WpfToolkit – Ahmed HM May 31 '21 at 16:10
  • I managed to get it working, downloaded "DotNetProjects.WpfToolkit.Input" instead. Thanks so much for the help! – TheLazyUnicorn May 31 '21 at 16:51