Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[iOS] CollectionView not applying "Selected" VisualState to pre-selec…
Browse files Browse the repository at this point in the history
…ted items (#14672)

* Fix issue setting initial selected VisualState in iOS CollectionView

* Updated the issue steps
  • Loading branch information
jsuarezruiz committed Nov 26, 2021
1 parent be2471c commit 8fcf863
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Test 14513" xmlns:local="using:Xamarin.Forms.Controls"
x:Class="Xamarin.Forms.Controls.Issues.Issue14513">
<StackLayout
Padding="12">
<Label
Text="If the selected item have a Red background, the test has passed."/>
<CollectionView
ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedItem}"
SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="CommonStates">
<VisualState Name="Normal" />
<VisualState Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Red" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Label
x:Name="Title"
Text="{Binding Name}"
FontSize="Large" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</local:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Collections.ObjectModel;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.CollectionView)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 14513,
"[Bug] [iOS] SelectedItems custom image not displaying in iOS for CollectionView until tapped",
PlatformAffected.iOS)]
public partial class Issue14513 : TestContentPage
{
public Issue14513()
{
#if APP
InitializeComponent();
#endif
}

protected override void Init()
{
BindingContext = new Issue14513ViewModel();
}
}

public class Issue14513Model
{
public string Name { get; set; }
}

public class Issue14513ViewModel : BindableObject
{
Issue14513Model _selectedItem;

public Issue14513ViewModel()
{
Items = new ObservableCollection<Issue14513Model>
{
new Issue14513Model { Name = "Item 1" },
new Issue14513Model { Name = "Item 2" },
new Issue14513Model { Name = "Item 3" }
};

SelectedItem = Items[1];
}

public ObservableCollection<Issue14513Model> Items { get; set; }

public Issue14513Model SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue13577.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14505.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14505-II.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14513.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue6387.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14757.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14897.xaml.cs" />
Expand Down Expand Up @@ -2354,6 +2355,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13577.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue14513.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue6387.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down
24 changes: 15 additions & 9 deletions Xamarin.Forms.Platform.iOS/CollectionView/TemplatedCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ void SetRenderer(IVisualElementRenderer renderer)

InitializeContentConstraints(nativeView);

UpdateVisualStates();

renderer.Element.MeasureInvalidated += MeasureInvalidated;
}

Expand Down Expand Up @@ -246,15 +248,7 @@ public override bool Selected
set
{
base.Selected = value;

var element = VisualElementRenderer?.Element;

if (element != null)
{
VisualStateManager.GoToState(element, value
? VisualStateManager.CommonStates.Selected
: VisualStateManager.CommonStates.Normal);
}
UpdateVisualStates();
}
}

Expand Down Expand Up @@ -304,5 +298,17 @@ bool SizesAreSame(CGSize preferredSize, Size elementSize)

return true;
}

void UpdateVisualStates()
{
var element = VisualElementRenderer?.Element;

if (element != null)
{
VisualStateManager.GoToState(element, Selected
? VisualStateManager.CommonStates.Selected
: VisualStateManager.CommonStates.Normal);
}
}
}
}

0 comments on commit 8fcf863

Please sign in to comment.