Skip to content
This repository has been archived by the owner on Feb 20, 2022. It is now read-only.

Commit

Permalink
Restrict "AutoCompleteField" to only on "AutoCompleteManager"
Browse files Browse the repository at this point in the history
In some rare cases it may happen, that the "Loaded" Event of the "TextBox" will be called twice. Because an "AutoCompleteManager" will be added to the "TextBox" each time the "Loaded" Event is fired, it may be added multiple times, resulting in having more than one "AutoCompleteManager". Which results in displaying multiple popups, when the User writes something.
  • Loading branch information
wgnf authored Aug 20, 2019
1 parent 3fea9df commit ecebb67
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions NZazu/Fields/NZazuAutocompleteField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace NZazu.Fields
internal class NZazuAutocompleteField : NZazuField<string>
{
private readonly IProvideSuggestions _suggester;
private bool _suggesterAttached;

public NZazuAutocompleteField(FieldDefinition definition, Func<Type, object> serviceLocatorFunc)
: base(definition, serviceLocatorFunc)
Expand All @@ -37,14 +38,20 @@ protected override Control CreateValueControl()

// we have to do this on load because some wpf stuff does not work if no parent is set
// i.e. some popup magic on window
result.Loaded += (sender, args) => { AttachSuggestor(sender); };
result.Loaded += (sender, args) =>
{
if (_suggesterAttached) return;
_suggesterAttached = true;

AttachSuggester(sender);
};

return result;
}

private void AttachSuggestor(object sender)
private void AttachSuggester(object sender)
{
// no suggester, no suggestions ;)
// no suggester, no suggestions ;)
if (_suggester == null) return;

var tb = (TextBox) sender;
Expand All @@ -59,4 +66,4 @@ private void AttachSuggestor(object sender)
manager.AttachTextBox(tb);
}
}
}
}

0 comments on commit ecebb67

Please sign in to comment.