Skip to content

Commit

Permalink
Merge pull request #1173 from mcNets/fluent-autosuggestbox
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund authored Oct 16, 2024
2 parents 57fdfbd + 0afbb51 commit f147f56
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions Uno.Gallery/Views/SamplePages/AutoSuggestBoxSamplePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using Uno.Gallery.ViewModels;
using Microsoft.UI.Xaml.Controls;
using Uno.Gallery.Helpers;
using Microsoft.UI.Xaml.Controls.Primitives;

namespace Uno.Gallery.Views.Samples
{
Expand All @@ -12,17 +14,30 @@ public sealed partial class AutoSuggestBoxSamplePage : Page
public AutoSuggestBoxSamplePage()
{
this.InitializeComponent();
Loaded += AutoSuggestBoxSamplePage_Loaded;
}

private void AutoSuggestBoxSamplePage_Loaded(object sender, RoutedEventArgs e)
{
#if HAS_UNO
var desc = VisualTreeHelperEx.GetDescendants(this).OfType<AutoSuggestBox>();

foreach (var item in desc)
{

var border = item.GetTemplateChild("SuggestionsContainer") as Border;
border?.EnsureXamlControlsResources(true);
}
#endif
Loaded -= AutoSuggestBoxSamplePage_Loaded;
}

private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
//This check can be removed when https://github.com/unoplatform/uno/issues/11635 is fixed
#if !__ANDROID__ && !__IOS__
if (args.Reason != AutoSuggestionBoxTextChangeReason.UserInput)
{
return;
}
#endif

if (string.IsNullOrEmpty(sender.Text))
{
Expand All @@ -31,28 +46,25 @@ private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTex

if (((Sample)DataContext).Data is AutoSuggestBoxSamplePageViewModel viewModel)
{
sender.ItemsSource = viewModel.GetSuggestedItems(sender.Text).Select(sample => sample.Title).ToList();
sender.ItemsSource = viewModel.GetSuggestedItems(sender.Text)?.Select(sample => sample.Title).ToList();
}
}

private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
if (((Sample)DataContext).Data is AutoSuggestBoxSamplePageViewModel viewModel)
{
viewModel.SelectedString = args.SelectedItem.ToString();
viewModel.SelectedString = args.SelectedItem.ToString() ?? string.Empty;
}

}

private void SearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
//This check can be removed when https://github.com/unoplatform/uno/issues/11635 is fixed
#if !__ANDROID__ && !__IOS__
if (args.Reason != AutoSuggestionBoxTextChangeReason.UserInput)
{
return;
}
#endif

if (((Sample)DataContext).Data is AutoSuggestBoxSamplePageViewModel viewModel)
{
Expand Down Expand Up @@ -88,7 +100,7 @@ public class AutoSuggestBoxSamplePageViewModel : ViewModelBase

public Sample SearchBoxSelectedItem { get => GetProperty<Sample>(); set => SetProperty(value); }

public List<Sample> GetSuggestedItems(string searchQuery)
public List<Sample>? GetSuggestedItems(string searchQuery)
{
if (string.IsNullOrEmpty(searchQuery))
{
Expand Down

0 comments on commit f147f56

Please sign in to comment.