Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add sample for nested ItemsView #19323

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<Page x:Class="UITests.Microsoft_UI_Xaml_Controls.ItemsViewTests.ItemsView_Nested"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Microsoft_UI_Xaml_Controls.ItemsViewTests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:winui="using:Microsoft.UI.Xaml.Controls"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<StackPanel>
<ItemsRepeater ItemsSource="{x:Bind Categories, Mode=OneWay}">
<ItemsRepeater.ItemTemplate>
<DataTemplate x:DataType="local:Category">
<ItemContainer>
<StackPanel DataContext="{x:Bind}">

<TextBlock Text="{x:Bind Label}"
HorizontalAlignment="Center"
FontSize="20"
Margin="0,10,0,10" />

<ItemsView ItemsSource="{x:Bind}">
<ItemsView.ItemTemplate>
<DataTemplate x:DataType="local:Item">
<ItemContainer>
<TextBlock Text="{x:Bind Label}"
HorizontalAlignment="Center" />
</ItemContainer>
</DataTemplate>
</ItemsView.ItemTemplate>
</ItemsView>

</StackPanel>
</ItemContainer>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>

<ItemsView ItemsSource="{x:Bind Categories}">
<ItemsView.ItemTemplate>
<DataTemplate x:DataType="local:Category">
<ItemContainer>
<TextBlock Text="{x:Bind Label}"
HorizontalAlignment="Center" />
</ItemContainer>
</DataTemplate>
</ItemsView.ItemTemplate>
</ItemsView>
</StackPanel>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Uno.UI.Samples.Controls;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace UITests.Microsoft_UI_Xaml_Controls.ItemsViewTests
{
[Sample("ItemsView", IgnoreInSnapshotTests = true)]
public sealed partial class ItemsView_Nested : Page
{
List<Category> Categories { get; set; }

public ItemsView_Nested()
{
InitializeComponent();

Categories = GenerateRandomCategories(5, 10);

Categories[0].Collapsed = !Categories[0].Collapsed;
}

private static List<Category> GenerateRandomCategories(int categoryCount, int maxItemsPerCategory)
{
var random = new Random();
var categories = new List<Category>();

for (int i = 1; i <= categoryCount; i++)
{
var itemCount = random.Next(1, maxItemsPerCategory + 1);
var items = Enumerable.Range(1, itemCount)
.Select(j => new Item($"Item {j}"))
.ToList();
var category = new Category($"Category {i}", items, random.Next(0, 2) == 0);
categories.Add(category);
}

return categories;
}
}

public partial class Item(string label)
{
public string Label { get; } = label;
}

public partial class Category : ObservableCollection<Item>
{
private bool _collapsed;

private readonly List<Item> _collapsedItems = [];

public string Label { get; }

public bool Collapsed
{
get => _collapsed;
set
{
if (_collapsed != value)
{
if (_collapsed)
{
foreach (Item item in _collapsedItems)
Add(item);
_collapsedItems.Clear();
}
else
{
_collapsedItems.AddRange(Items);
Clear();
}
_collapsed = value;
}
}
}

public Category(string label, IEnumerable<Item> items, bool collapsed = false) : base(items)
{
Label = label;
Collapsed = collapsed;
}
}
}
Loading