I have searched Google for a simple solution to this but no luck. I have a standard WPF combo box which I would simply like to be able to filter the list displayed according to the first 2 or 3 letters a users types when the combo box has focus. I tried some coding including some lamba expressions but the error "System.NotSupportedException" keeps getting thrown on the line where "combobox.Items.Filter" is specified. I'm not using MVVM and would just like this simple functionality available for the user. Please help! P.S. IsEditable, IsTextSearchEnabled and StaysOpenOnEdit properties are set to true but the desired functionality is not yet achieved.
Asked
Active
Viewed 2.6k times
8
-
you want to let programm predict from the available list in the programm na .. – Ankur Jyoti Phukan Dec 20 '15 at 15:23
-
The combo box is populated using a data table but the list has a couple of hundred options. So when the user types a few characters I need the list to be shortened and open for selection. – BabyDoll Dec 20 '15 at 15:29
-
I think there some error with query check this ... [link](http://stackoverflow.com/questions/25337974/entity-framework-notsupportedexception-in-lambda-expression) – Ankur Jyoti Phukan Dec 20 '15 at 16:21
-
Sorry I'm not following, what query are you revering to? – BabyDoll Dec 20 '15 at 17:17
-
check that link .... i think you have similar problem – Ankur Jyoti Phukan Dec 20 '15 at 17:18
-
My error occurs when I try to implement the .Items.Filter method, nor sure how that link pertains to my problem or is it just too late on a Sunday evening? – BabyDoll Dec 20 '15 at 17:40
2 Answers
24
I have developed a sample application. I have used string as record item, you can do it using your own entity. Backspace also works properly.
public class FilterViewModel
{
public IEnumerable<string> DataSource { get; set; }
public FilterViewModel()
{
DataSource = new[] { "india", "usa", "uk", "indonesia" };
}
}
public partial class WinFilter : Window
{
public WinFilter()
{
InitializeComponent();
FilterViewModel vm = new FilterViewModel();
this.DataContext = vm;
}
private void Cmb_KeyUp(object sender, KeyEventArgs e)
{
CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(Cmb.ItemsSource);
itemsViewOriginal.Filter = ((o) =>
{
if (String.IsNullOrEmpty(Cmb.Text)) return true;
else
{
if (((string)o).Contains(Cmb.Text)) return true;
else return false;
}
});
itemsViewOriginal.Refresh();
// if datasource is a DataView, then apply RowFilter as below and replace above logic with below one
/*
DataView view = (DataView) Cmb.ItemsSource;
view.RowFilter = ("Name like '*" + Cmb.Text + "*'");
*/
}
}
XAML
<ComboBox x:Name="Cmb"
IsTextSearchEnabled="False"
IsEditable="True"
ItemsSource="{Binding DataSource}"
Width="120"
IsDropDownOpen="True"
StaysOpenOnEdit="True"
KeyUp="Cmb_KeyUp" />

AwkwardCoder
- 24,893
- 27
- 82
- 152

AnjumSKhan
- 9,647
- 1
- 26
- 38
-
With both replies I still get the "System.NotSupportedException". I have even tried creating a custom control inheriting from the combo box and still the same error. Is there perhaps a third party tool that anyone can recommend? This is driving me insane! – BabyDoll Jan 04 '16 at 10:55
-
@BabyDoll u must be doing something silly. u can upload your code at dropbox.com and share the link. – AnjumSKhan Jan 04 '16 at 14:04
-
I think so too!! Perhaps it is worth mentioning that I am binding directly from a datatable? Total beginner at WPF so I'm sure there is something stupid that I am missing. I read somewhere that datatables do not support filtering (the way I am doing it), but isn't that why I should use the ICollectionView in the first place? – BabyDoll Jan 04 '16 at 14:12
-
@BabyDoll what u r getting in 'itemsViewOriginal` `CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(Cmb.ItemsSource);` – AnjumSKhan Jan 04 '16 at 16:11
-
When I debug I see the contents of the datatable with the correct data. Something just occurred to me; I'm trying to filter data but I don't specify which column to use....is this my problem?? And if so, how do I specify which column to use for filtering? – BabyDoll Jan 04 '16 at 16:37
-
@BabyDoll in this line `if (((string)o).Contains(Cmb.Text)) return true;`, `o` gives you the object/database-row/record, you can get the column/property from it. – AnjumSKhan Jan 04 '16 at 16:56
-
the coding doesn't reach that point. The section of coding that starts at "itemsViewOriginal.Filter = ((o) =>" is where the error occurs. – BabyDoll Jan 05 '16 at 06:15
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99765/discussion-between-anjumskhan-and-babydoll). – AnjumSKhan Jan 05 '16 at 06:25
-
The filter works great! But whenever I try to use the arrow keys nothing happends. Any suggestions on that @AnjumSKhan? – BabyDoll Jan 06 '16 at 05:55
-
I got a "StackOverflowException" when I used this in MVVM. Removing "itemsViewOriginal.Refresh();" solved it for me. – Fred Jun 15 '17 at 20:12
-
Try to remove text or change text after selecting item. It will prevent changing Text after selection done. – AEMLoviji Nov 14 '18 at 13:18
0
I think the CollectionView is what you are looking for.
public ObservableCollection<NdfClassViewModel> Classes
{
get { return _classes; }
}
public ICollectionView ClassesCollectionView
{
get
{
if (_classesCollectionView == null)
{
BuildClassesCollectionView();
}
return _classesCollectionView;
}
}
private void BuildClassesCollectionView()
{
_classesCollectionView = CollectionViewSource.GetDefaultView(Classes);
_classesCollectionView.Filter = FilterClasses;
OnPropertyChanged(() => ClassesCollectionView);
}
public bool FilterClasses(object o)
{
var clas = o as NdfClassViewModel;
// return true if object should be in list with applied filter, return flase if not
}
You wanna use the "ClassesCollectionView" as your ItemsSource for your Combobox

Ian
- 323
- 3
- 8
-
Thanks for the reply! But I'm still getting the error on the line where combobox.Items.Filter is specified. – BabyDoll Dec 21 '15 at 06:52