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" />
</Style>
</UniformGrid.Styles>
<StackPanel>
<TextBlock Text="MinimumPrefixLength: 0" />
<AutoCompleteBox MinimumPrefixLength="0" />
</StackPanel>
<StackPanel>
<TextBlock Text="MinimumPrefixLength: 1" />
<AutoCompleteBox MinimumPrefixLength="1" />
@ -42,7 +46,6 @@
<TextBlock Text="Disabled" />
<AutoCompleteBox IsEnabled="False" />
</StackPanel>
<StackPanel>
<TextBlock Text="ValueMemberBinding" />
<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.
// 1. Minimum prefix length
// 2. If a delay timer is in use, use it
bool populateReady = newText.Length >= MinimumPrefixLength && MinimumPrefixLength >= 0;
if (populateReady && MinimumPrefixLength == 0 && String.IsNullOrEmpty(newText) && String.IsNullOrEmpty(SearchText))
{
populateReady = false;
}
_userCalledPopulate = populateReady ? userInitiated : false;
bool minimumLengthReached = newText.Length >= MinimumPrefixLength && MinimumPrefixLength >= 0;
_userCalledPopulate = minimumLengthReached && userInitiated;
// Update the interface and values only as necessary
UpdateTextValue(newText, userInitiated);
if (populateReady)
if (minimumLengthReached)
{
_ignoreTextSelectionChange = true;

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

@ -1,21 +1,15 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Markup.Data;
using Avalonia.Platform;
using Avalonia.Threading;
using Avalonia.UnitTests;
using Moq;
using Xunit;
using System.Collections.ObjectModel;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using Avalonia.Input;
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>
/// Retrieves a defined predicate filter through a new AutoCompleteBox
/// control instance.
@ -1072,14 +1089,14 @@ namespace Avalonia.Controls.UnitTests
private AutoCompleteBox CreateControl()
{
var datePicker =
var autoCompleteBox =
new AutoCompleteBox
{
Template = CreateTemplate()
};
datePicker.ApplyTemplate();
return datePicker;
autoCompleteBox.ApplyTemplate();
return autoCompleteBox;
}
private TextBox GetTextBox(AutoCompleteBox control)
{

Loading…
Cancel
Save