Browse Source

Merge pull request #12057 from AtomCrafty/autocompletebox

Fix AutoCompleteBox not opening when the text box is empty
pull/12070/head
Max Katz 3 years ago
committed by GitHub
parent
commit
c444b4b278
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      samples/ControlCatalog/Pages/AutoCompleteBoxPage.xaml
  2. 11
      src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs
  3. 37
      tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs

5
samples/ControlCatalog/Pages/AutoCompleteBoxPage.xaml

@ -18,6 +18,10 @@
<Setter Property="Margin" Value="8" /> <Setter Property="Margin" Value="8" />
</Style> </Style>
</UniformGrid.Styles> </UniformGrid.Styles>
<StackPanel>
<TextBlock Text="MinimumPrefixLength: 0" />
<AutoCompleteBox MinimumPrefixLength="0" />
</StackPanel>
<StackPanel> <StackPanel>
<TextBlock Text="MinimumPrefixLength: 1" /> <TextBlock Text="MinimumPrefixLength: 1" />
<AutoCompleteBox MinimumPrefixLength="1" /> <AutoCompleteBox MinimumPrefixLength="1" />
@ -42,7 +46,6 @@
<TextBlock Text="Disabled" /> <TextBlock Text="Disabled" />
<AutoCompleteBox IsEnabled="False" /> <AutoCompleteBox IsEnabled="False" />
</StackPanel> </StackPanel>
<StackPanel> <StackPanel>
<TextBlock Text="ValueMemberBinding" /> <TextBlock Text="ValueMemberBinding" />
<AutoCompleteBox ValueMemberBinding="{Binding Capital, x:DataType=models:StateData}" /> <AutoCompleteBox ValueMemberBinding="{Binding Capital, x:DataType=models:StateData}" />

11
src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs

@ -1320,17 +1320,14 @@ namespace Avalonia.Controls
// Evaluate the conditions needed for completion. // Evaluate the conditions needed for completion.
// 1. Minimum prefix length // 1. Minimum prefix length
// 2. If a delay timer is in use, use it // 2. If a delay timer is in use, use it
bool populateReady = newText.Length >= MinimumPrefixLength && MinimumPrefixLength >= 0; bool minimumLengthReached = newText.Length >= MinimumPrefixLength && MinimumPrefixLength >= 0;
if (populateReady && MinimumPrefixLength == 0 && String.IsNullOrEmpty(newText) && String.IsNullOrEmpty(SearchText))
{ _userCalledPopulate = minimumLengthReached && userInitiated;
populateReady = false;
}
_userCalledPopulate = populateReady ? userInitiated : false;
// Update the interface and values only as necessary // Update the interface and values only as necessary
UpdateTextValue(newText, userInitiated); UpdateTextValue(newText, userInitiated);
if (populateReady) if (minimumLengthReached)
{ {
_ignoreTextSelectionChange = true; _ignoreTextSelectionChange = true;

37
tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs

@ -1,21 +1,15 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Avalonia.Data; using Avalonia.Data;
using Avalonia.Markup.Data;
using Avalonia.Platform;
using Avalonia.Threading; using Avalonia.Threading;
using Avalonia.UnitTests; using Avalonia.UnitTests;
using Moq;
using Xunit; using Xunit;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Reactive.Linq;
using System.Reactive.Subjects; using System.Reactive.Subjects;
using Avalonia.Input;
namespace Avalonia.Controls.UnitTests namespace Avalonia.Controls.UnitTests
{ {
@ -439,6 +433,29 @@ namespace Avalonia.Controls.UnitTests
}); });
} }
[Fact]
public void Explicit_Dropdown_Open_Request_MinimumPrefixLength_0()
{
RunTest((control, textbox) =>
{
control.Text = "";
control.MinimumPrefixLength = 0;
Dispatcher.UIThread.RunJobs();
Assert.False(control.IsDropDownOpen);
control.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.Down
});
Dispatcher.UIThread.RunJobs();
Assert.True(control.IsDropDownOpen);
});
}
/// <summary> /// <summary>
/// Retrieves a defined predicate filter through a new AutoCompleteBox /// Retrieves a defined predicate filter through a new AutoCompleteBox
/// control instance. /// control instance.
@ -1072,14 +1089,14 @@ namespace Avalonia.Controls.UnitTests
private AutoCompleteBox CreateControl() private AutoCompleteBox CreateControl()
{ {
var datePicker = var autoCompleteBox =
new AutoCompleteBox new AutoCompleteBox
{ {
Template = CreateTemplate() Template = CreateTemplate()
}; };
datePicker.ApplyTemplate(); autoCompleteBox.ApplyTemplate();
return datePicker; return autoCompleteBox;
} }
private TextBox GetTextBox(AutoCompleteBox control) private TextBox GetTextBox(AutoCompleteBox control)
{ {

Loading…
Cancel
Save