Skip to content

Commit bb8eba0

Browse files
Shalini-AshokanPureWeen
authored andcommitted
Fix CarouselView ItemTemplate Runtime Updates (#29546)
* Fixed the item template dynamic changes * Added a test case * Added test case and added snapshots * committed the image * committed the image
1 parent accc591 commit bb8eba0

File tree

7 files changed

+122
-0
lines changed

7 files changed

+122
-0
lines changed

src/Controls/src/Core/Handlers/Items/CarouselViewHandler.Windows.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ protected override void UpdateItemTemplate()
8787
return;
8888

8989
ListViewBase.ItemTemplate = CarouselItemsViewTemplate;
90+
UpdateItemsSource();
9091
}
9192

9293
protected override void OnScrollViewerFound(ScrollViewer scrollViewer)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System.Collections.ObjectModel;
2+
3+
namespace Maui.Controls.Sample.Issues;
4+
5+
[Issue(IssueTracker.Github, 29462, "CarouselView ItemTemplate Not Updating at Runtime", PlatformAffected.UWP)]
6+
public class Issue29462 : ContentPage
7+
{
8+
public Issue29462()
9+
{
10+
var verticalStackLayout = new VerticalStackLayout();
11+
var carouselItems = new ObservableCollection<string>
12+
{
13+
"Item 0",
14+
"Item 1",
15+
"Item 2",
16+
"Item 3",
17+
"Item 4",
18+
};
19+
20+
CarouselView2 carouselView = new CarouselView2
21+
{
22+
ItemsSource = carouselItems,
23+
ItemsUpdatingScrollMode = ItemsUpdatingScrollMode.KeepScrollOffset,
24+
HeightRequest = 300,
25+
Loop = false,
26+
HorizontalScrollBarVisibility = ScrollBarVisibility.Never,
27+
ItemTemplate = new DataTemplate(() =>
28+
{
29+
var grid = new Grid
30+
{
31+
Padding = 10
32+
};
33+
34+
var label = new Label
35+
{
36+
VerticalOptions = LayoutOptions.Center,
37+
HorizontalOptions = LayoutOptions.Center,
38+
FontSize = 18,
39+
};
40+
label.SetBinding(Label.TextProperty, ".");
41+
label.SetBinding(Label.AutomationIdProperty, ".");
42+
43+
grid.Children.Add(label);
44+
return grid;
45+
}),
46+
HorizontalOptions = LayoutOptions.Fill,
47+
};
48+
49+
var indicatorView = new IndicatorView
50+
{
51+
HorizontalOptions = LayoutOptions.Center,
52+
VerticalOptions = LayoutOptions.Center
53+
};
54+
carouselView.IndicatorView = indicatorView;
55+
56+
var itemTempateButton = new Button
57+
{
58+
Text = "Change Item Template",
59+
AutomationId = "ChangeItemTemplate",
60+
Margin = new Thickness(20),
61+
};
62+
63+
itemTempateButton.Clicked += (sender, e) =>
64+
{
65+
carouselView.ItemTemplate = new DataTemplate(() =>
66+
{
67+
Grid grid = new Grid
68+
{
69+
BackgroundColor = Colors.LightGray,
70+
HeightRequest = 200,
71+
WidthRequest = 300,
72+
Margin = new Thickness(10)
73+
};
74+
75+
var label = new Label
76+
{
77+
HorizontalOptions = LayoutOptions.Center,
78+
VerticalOptions = LayoutOptions.Center,
79+
FontSize = 24,
80+
TextColor = Colors.DarkBlue
81+
};
82+
83+
label.SetBinding(Label.TextProperty, new Binding(".")
84+
{
85+
StringFormat = "{0} (Grid Template)"
86+
});
87+
88+
grid.Children.Add(label);
89+
return grid;
90+
});
91+
};
92+
93+
verticalStackLayout.Children.Add(carouselView);
94+
verticalStackLayout.Children.Add(indicatorView);
95+
verticalStackLayout.Children.Add(itemTempateButton);
96+
Content = verticalStackLayout;
97+
}
98+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue29462 : _IssuesUITest
8+
{
9+
public override string Issue => "CarouselView ItemTemplate Not Updating at Runtime";
10+
11+
public Issue29462(TestDevice device)
12+
: base(device)
13+
{ }
14+
15+
[Test]
16+
[Category(UITestCategories.CarouselView)]
17+
public void TestDynamicItemTemplateChangeInCarouselView()
18+
{
19+
App.WaitForElement("ChangeItemTemplate");
20+
App.Tap("ChangeItemTemplate");
21+
VerifyScreenshot();
22+
}
23+
}

0 commit comments

Comments
 (0)