From 05381e0358911852cabdb74a63f325b4e07e0ca5 Mon Sep 17 00:00:00 2001 From: Hendrik Mennen Date: Thu, 1 Aug 2024 21:11:38 +0200 Subject: [PATCH] fix searchccombobox --- .../Controls/SearchComboBox.axaml | 3 --- .../Controls/SearchComboBox.axaml.cs | 27 ++++++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/OneWare.Essentials/Controls/SearchComboBox.axaml b/src/OneWare.Essentials/Controls/SearchComboBox.axaml index 88d3a1ee..195ebfd3 100644 --- a/src/OneWare.Essentials/Controls/SearchComboBox.axaml +++ b/src/OneWare.Essentials/Controls/SearchComboBox.axaml @@ -87,9 +87,6 @@ - diff --git a/src/OneWare.Essentials/Controls/SearchComboBox.axaml.cs b/src/OneWare.Essentials/Controls/SearchComboBox.axaml.cs index a72690a5..ca99445d 100644 --- a/src/OneWare.Essentials/Controls/SearchComboBox.axaml.cs +++ b/src/OneWare.Essentials/Controls/SearchComboBox.axaml.cs @@ -32,6 +32,11 @@ public int ResultIndex set { _resultIndex = value; + if(value < 0) + { + ResultItem = null; + return; + } ResultItem = ContainerFromIndex(value) as SearchComboBoxItem; ScrollIntoView(value); } @@ -62,21 +67,25 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e) _searchBox!.TextChanged += (sender, args) => { - var item = Items.FirstOrDefault(x => - x?.ToString()?.StartsWith(_searchBox.Text ?? string.Empty, StringComparison.OrdinalIgnoreCase) - ?? x?.ToString()?.Contains(_searchBox.Text ?? string.Empty, StringComparison.OrdinalIgnoreCase) - ?? false); - + object? item = null; + if (!string.IsNullOrWhiteSpace(_searchBox.Text)) + { + item = Items.FirstOrDefault(x => + x?.ToString()?.StartsWith(_searchBox.Text ?? string.Empty, StringComparison.OrdinalIgnoreCase) ?? + false); + + item ??= Items.FirstOrDefault(x => + x?.ToString()?.Contains(_searchBox.Text ?? string.Empty, StringComparison.OrdinalIgnoreCase) ?? + false); + } + if (IsInteractive) { SelectedItem = item; } else { - if (item != null) - { - ResultIndex = Items.IndexOf(item); - } + ResultIndex = Items.IndexOf(item); } _searchBox.Focus();