Skip to content

Commit

Permalink
Fix initial selection for AlwaysSelected (#14668)
Browse files Browse the repository at this point in the history
#Conflicts:
#	tests/Avalonia.Controls.UnitTests/TabControlTests.cs
  • Loading branch information
Gillibald authored and maxkatz6 committed Feb 22, 2024
1 parent 027d1ea commit 998b7a8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,12 @@ private protected override void OnItemsViewCollectionChanged(object? sender, Not
{
base.OnItemsViewCollectionChanged(sender!, e);

//Do not change SelectedIndex during initialization
if (_updateState is not null)
{
return;
}

if (AlwaysSelected && SelectedIndex == -1 && ItemCount > 0)
{
SelectedIndex = 0;
Expand Down Expand Up @@ -1217,7 +1223,7 @@ private void InitializeSelectionModel(ISelectionModel model)
_oldSelectedIndex = model.SelectedIndex;
_oldSelectedItem = model.SelectedItem;

if (AlwaysSelected && model.Count == 0)
if (_updateState is null && AlwaysSelected && model.Count == 0)
{
model.SelectedIndex = 0;
}
Expand Down Expand Up @@ -1297,6 +1303,11 @@ private void EndUpdating()
{
SelectedItem = state.SelectedItem.Value;
}

if (AlwaysSelected && SelectedIndex == -1 && ItemCount > 0)
{
SelectedIndex = 0;
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions tests/Avalonia.Controls.UnitTests/TabControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using Avalonia.Collections;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Selection;
using Avalonia.Controls.Templates;
using Avalonia.Controls.Utils;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml;
Expand All @@ -19,6 +21,11 @@ namespace Avalonia.Controls.UnitTests
{
public class TabControlTests
{
static TabControlTests()
{
RuntimeHelpers.RunClassConstructor(typeof(RelativeSource).TypeHandle);
}

[Fact]
public void First_Tab_Should_Be_Selected_By_Default()
{
Expand Down Expand Up @@ -436,6 +443,29 @@ public void Can_Have_Empty_Tab_Control()
}
}

[Fact]
public void Should_Have_Initial_SelectedValue()
{
var xaml = @"
<TabControl
xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'
x:DataType='TabItem'
x:Name='tabs'
Tag='World'
SelectedValue='{Binding $self.Tag}'
SelectedValueBinding='{Binding Header}'>
<TabItem Header='Hello'/>
<TabItem Header='World'/>
</TabControl>";

var tabControl = (TabControl)AvaloniaRuntimeXamlLoader.Load(xaml);

Assert.Equal("World", tabControl.SelectedValue);
Assert.Equal(1, tabControl.SelectedIndex);
}

[Fact]
public void Tab_Navigation_Should_Move_To_First_TabItem_When_No_Anchor_Element_Selected()
{
Expand Down

0 comments on commit 998b7a8

Please sign in to comment.