Skip to content

Commit

Permalink
fix: AutoSuggestBox.QueryIcon does not work
Browse files Browse the repository at this point in the history
- Fixes #5545
  • Loading branch information
MartinZikmund committed Mar 31, 2021
1 parent 0d7ff55 commit 1486945
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public string Text
Windows.UI.Xaml.DependencyProperty.Register(
"QueryIcon", typeof(global::Windows.UI.Xaml.Controls.IconElement),
typeof(global::Windows.UI.Xaml.Controls.AutoSuggestBox),
new FrameworkPropertyMetadata(default(global::Windows.UI.Xaml.Controls.IconElement)));
new FrameworkPropertyMetadata(default(global::Windows.UI.Xaml.Controls.IconElement), propertyChangedCallback: (s, e) => (s as AutoSuggestBox)?.UpdateQueryButton()));

public event TypedEventHandler<AutoSuggestBox, AutoSuggestBoxSuggestionChosenEventArgs> SuggestionChosen;
public event TypedEventHandler<AutoSuggestBox, AutoSuggestBoxTextChangedEventArgs> TextChanged;
Expand Down
42 changes: 29 additions & 13 deletions src/Uno.UI/UI/Xaml/Controls/AutoSuggestBox/AutoSuggestBox.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@

using System;
using System.Collections.Specialized;
using System.Linq;
using Uno.Extensions;
using Uno.Extensions.Specialized;
using Uno.Logging;
using Uno.UI;
using Uno.UI.DataBinding;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using System.Collections.Specialized;
using Uno.UI.DataBinding;

#if __IOS__
using UIKit;
Expand All @@ -21,7 +20,7 @@

namespace Windows.UI.Xaml.Controls
{
public partial class AutoSuggestBox : ItemsControl, IValueChangedListener
public partial class AutoSuggestBox : ItemsControl, IValueChangedListener
{
private TextBox _textBox;
private Popup _popup;
Expand Down Expand Up @@ -53,12 +52,9 @@ protected override void OnApplyTemplate()
_popup.DisableFocus();
#endif

if (_queryButton != null)
{
_queryButton.Content = new SymbolIcon(Symbol.Find);
}
UpdateQueryButton();

_textBoxBinding = new BindingPath("Text", null) {DataContext = _textBox, ValueChangedListener = this};
_textBoxBinding = new BindingPath("Text", null) { DataContext = _textBox, ValueChangedListener = this };

Loaded += (s, e) => RegisterEvents();
Unloaded += (s, e) => UnregisterEvents();
Expand Down Expand Up @@ -276,6 +272,24 @@ private void OnIsSuggestionListOpenChanged(DependencyPropertyChangedEventArgs e)
}
}

private void UpdateQueryButton()
{
if (_queryButton == null)
{
return;
}

_queryButton.Content = QueryIcon;
if (QueryIcon == null)
{
_queryButton.Visibility = Visibility.Collapsed;
}
else
{
_queryButton.Visibility = Visibility.Visible;
}
}

private void OnSuggestionListItemClick(object sender, ItemClickEventArgs e)
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
Expand Down Expand Up @@ -322,7 +336,8 @@ private void OnTextBoxKeyDown(object sender, KeyRoutedEventArgs e)
{
RevertTextToUserInput();
IsSuggestionListOpen = false;
} else
}
else
{
_textChangeReason = AutoSuggestionBoxTextChangeReason.UserInput;
}
Expand Down Expand Up @@ -351,7 +366,8 @@ private void HandleUpDownKeys(KeyRoutedEventArgs e)
if (nextIndex == -1)
{
RevertTextToUserInput();
} else
}
else
{
ChoseSuggestion();
}
Expand Down Expand Up @@ -391,7 +407,7 @@ private string GetObjectText(Object o)

if (TextMemberPath != null)
{
using var bindingPath = new BindingPath(TextMemberPath, "", null, allowPrivateMembers: true) {DataContext = o};
using var bindingPath = new BindingPath(TextMemberPath, "", null, allowPrivateMembers: true) { DataContext = o };
value = bindingPath.Value;
}

Expand All @@ -402,7 +418,7 @@ private static void OnTextChanged(DependencyObject dependencyObject, DependencyP
{
var newValue = args.NewValue as string ?? string.Empty;

if(dependencyObject is AutoSuggestBox tb)
if (dependencyObject is AutoSuggestBox tb)
{
if (tb._textChangeReason == AutoSuggestionBoxTextChangeReason.UserInput)
{
Expand Down

0 comments on commit 1486945

Please sign in to comment.