Browse Source

reimplementation and some tests

pull/7963/head
Tako 4 years ago
parent
commit
fd5e2169c1
  1. 19
      src/Avalonia.Controls/ComboBox.cs
  2. 1
      src/Avalonia.Controls/ItemsControl.cs
  3. 2
      src/Avalonia.Controls/StackPanel.cs
  4. 75
      tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

19
src/Avalonia.Controls/ComboBox.cs

@ -455,24 +455,7 @@ namespace Avalonia.Controls
{
if (ItemCount >= 1)
{
if (SelectedIndex == -1)
{
if (MoveSelection(NavigationDirection.First, WrapSelection) == false)
{
// MoveSelection works only with indexes starting from 0
// so to make it search further than the first item we need to set SelectedIndex to 0.
SelectedIndex = 0;
var isSelectionMoved = MoveSelection(NavigationDirection.Next, WrapSelection);
if (isSelectionMoved == false)
{
SelectedIndex = -1;
}
}
}
else
{
MoveSelection(NavigationDirection.Next, WrapSelection);
}
MoveSelection(NavigationDirection.Next, WrapSelection);
}
}

1
src/Avalonia.Controls/ItemsControl.cs

@ -508,7 +508,6 @@ namespace Avalonia.Controls
do
{
result = container.GetControl(direction, c, wrap);
from = from ?? result;
if (result != null &&
result.Focusable &&

2
src/Avalonia.Controls/StackPanel.cs

@ -123,7 +123,7 @@ namespace Avalonia.Controls
index = Children.Count - 1;
break;
case NavigationDirection.Next:
if (index != -1) ++index;
++index;
break;
case NavigationDirection.Previous:
if (index != -1) --index;

75
tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

@ -36,6 +36,81 @@ namespace Avalonia.Controls.UnitTests
Assert.False(target.IsDropDownOpen);
}
[Fact]
public void WrapSelection_Should_Work()
{
using (UnitTestApplication.Start(TestServices.RealFocus))
{
var items = new[]
{
new ComboBoxItem() { Content = "bla" },
new ComboBoxItem() { Content = "dd" },
new ComboBoxItem() { Content = "sdf", IsEnabled = false }
};
var target = new ComboBox
{
Items = items,
Template = GetTemplate(),
WrapSelection = true
};
var root = new TestRoot(target);
target.ApplyTemplate();
target.Presenter.ApplyTemplate();
target.Focus();
Assert.Equal(target.SelectedIndex, -1);
Assert.True(target.IsFocused);
target.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.Up,
});
Assert.Equal(target.SelectedIndex, 1);
target.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.Down,
});
Assert.Equal(target.SelectedIndex, 0);
}
}
[Fact]
public void Focuses_Next_Item_On_Key_Down()
{
using (UnitTestApplication.Start(TestServices.RealFocus))
{
var items = new[]
{
new ComboBoxItem() { Content = "bla" },
new ComboBoxItem() { Content = "dd", IsEnabled = false },
new ComboBoxItem() { Content = "sdf" }
};
var target = new ComboBox
{
Items = items,
Template = GetTemplate()
};
var root = new TestRoot(target);
target.ApplyTemplate();
target.Presenter.ApplyTemplate();
target.Focus();
Assert.Equal(target.SelectedIndex, -1);
Assert.True(target.IsFocused);
target.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.Down,
});
Assert.Equal(target.SelectedIndex, 0);
target.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.Down,
});
Assert.Equal(target.SelectedIndex, 2);
}
}
[Fact]
public void SelectionBoxItem_Is_Rectangle_With_VisualBrush_When_Selection_Is_Control()
{

Loading…
Cancel
Save