forked from MaterialDesignInXAML/MaterialDesignInXamlToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoSuggestTextBoxWithCollectionView.xaml.cs
More file actions
66 lines (57 loc) · 1.75 KB
/
AutoSuggestTextBoxWithCollectionView.xaml.cs
File metadata and controls
66 lines (57 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
using CommunityToolkit.Mvvm.ComponentModel;
namespace MaterialDesignThemes.UITests.Samples.AutoSuggestBoxes;
/// <summary>
/// Interaction logic for AutoSuggestTextBoxWithCollectionView.xaml
/// </summary>
public partial class AutoSuggestTextBoxWithCollectionView
{
public AutoSuggestTextBoxWithCollectionView()
{
DataContext = new AutoSuggestTextBoxWithCollectionViewViewModel();
InitializeComponent();
}
}
public partial class AutoSuggestTextBoxWithCollectionViewViewModel : ObservableObject
{
private ObservableCollection<string> BaseSuggestions { get; }
public ICollectionView Suggestions { get; }
[ObservableProperty]
private string? _autoSuggestText;
[ObservableProperty]
private string? _selectedItem;
partial void OnAutoSuggestTextChanged(string? oldValue, string? newValue)
{
if (!string.IsNullOrWhiteSpace(newValue))
{
Suggestions.Filter = x => IsMatch((string)x, newValue);
}
else
{
Suggestions.Filter = null;
}
OnPropertyChanged(nameof(Suggestions));
}
public AutoSuggestTextBoxWithCollectionViewViewModel()
{
BaseSuggestions =
[
"Apples",
"Bananas",
"Beans",
"Mtn Dew",
"Orange",
];
Suggestions = CollectionViewSource.GetDefaultView(BaseSuggestions);
}
private static bool IsMatch(string item, string currentText)
{
#if NET6_0_OR_GREATER
return item.Contains(currentText, StringComparison.OrdinalIgnoreCase);
#else
return item.IndexOf(currentText, StringComparison.OrdinalIgnoreCase) >= 0;
#endif
}
}