forked from MaterialDesignInXAML/MaterialDesignInXamlToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoSuggestTextBoxWithTemplate.xaml.cs
More file actions
68 lines (59 loc) · 1.82 KB
/
AutoSuggestTextBoxWithTemplate.xaml.cs
File metadata and controls
68 lines (59 loc) · 1.82 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
67
68
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using Google.Protobuf.WellKnownTypes;
namespace MaterialDesignThemes.UITests.Samples.AutoSuggestTextBoxes;
/// <summary>
/// Interaction logic for AutoSuggestTextBoxWithTemplate.xaml
/// </summary>
public partial class AutoSuggestTextBoxWithTemplate
{
public AutoSuggestTextBoxWithTemplate()
{
DataContext = new AutoSuggestTextBoxWithTemplateViewModel();
InitializeComponent();
}
}
public partial class AutoSuggestTextBoxWithTemplateViewModel : ObservableObject
{
private List<SuggestionThing> BaseSuggestions { get; }
[ObservableProperty]
private ObservableCollection<SuggestionThing> _suggestions = [];
[ObservableProperty]
private string? _autoSuggestText;
partial void OnAutoSuggestTextChanged(string? oldValue, string? newValue)
{
if (!string.IsNullOrWhiteSpace(newValue))
{
var searchResult = BaseSuggestions.Where(x => IsMatch(x.Name, newValue));
Suggestions = new(searchResult);
}
else
{
Suggestions = new(BaseSuggestions);
}
}
public AutoSuggestTextBoxWithTemplateViewModel()
{
BaseSuggestions =
[
new("Apples"),
new("Bananas"),
new("Beans"),
new("Mtn Dew"),
new("Orange"),
];
Suggestions = new ObservableCollection<SuggestionThing>(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
}
}
public class SuggestionThing(string name)
{
public string Name { get; } = name;
}