Repository navigation
Description
Problem
When Sample Data is enabled in the Visual Studio XAML Previewer (zateutsch-xaml-previewer, v0.3.15), a grouped ListView bound to a CollectionViewSource with IsSourceGrouped="True" and ItemsPath="Members" ignores the planner's generated sample rows ("Sample item 1", "Sample item 2", "Sample item 3") whenever the item class has a non-empty C# property initializer (e.g., public string Name { get; set; } = "Runtime item";). Every child row across all groups displays "Runtime item" instead.
Smallest repro
- Define a group class with an
ItemsPathcollection and an item class with a default property initializer:public sealed class SampleItem { public string Name { get; set; } = "Runtime item"; } public sealed class ItemGroup { public string Name { get; set; } = ""; public ObservableCollection<SampleItem> Members { get; set; } = []; }
- Bind a
ListViewthrough aCollectionViewSourcewithItemsPath="Members":<Page.Resources> <CollectionViewSource x:Key="PathGroups" Source="{x:Bind Model.Groups}" IsSourceGrouped="True" ItemsPath="Members"/> <DataTemplate x:Key="GroupRow" x:DataType="m:SampleItem"> <TextBlock Text="{x:Bind Name}"/> </DataTemplate> </Page.Resources> <ListView ItemsSource="{Binding Source={StaticResource PathGroups}}" ItemTemplate="{StaticResource GroupRow}"/>
- Open the page in the XAML Previewer and toggle Sample Data: On.
- Actual result: Group headers show
"Sample group 1"and"Sample group 2", but every child item in every group shows"Runtime item". - Expected result: Child items in each group show the planner's generated sample values (
"Sample item 1","Sample item 2","Sample item 3"), matching the behavior ofIList-subclass groups.
Root cause & smallest fix
In surface/Surface/SampleListFiller.cs (BuildGroups, lines 273–307):
var items = new List<object>();
if (g.TryGetProperty("Items", out var rowsOfGroup) && rowsOfGroup.ValueKind == JsonValueKind.Array)
{
int ii = 0;
foreach (var row in rowsOfGroup.EnumerateArray())
{
if (Convert(row) is not { } v) continue;
if (itemType is not null && SampleObjects.Create(itemType, v as IReadOnlyDictionary<string, object>, ii) is { } obj) v = obj;
items.Add(v);
ii++;
}
}
object? group = null;
if (groupType is not null &&
SampleObjects.Create(groupType, new Dictionary<string, object> { ["Key"] = key }, gi) is { } typed)
{
if (string.IsNullOrEmpty(itemsPath) && typed is System.Collections.IList l && !l.IsReadOnly && !l.IsFixedSize)
{
l.Clear();
foreach (var item in items) l.Add(item);
group = typed;
}
else if (!string.IsNullOrEmpty(itemsPath))
{
group = typed; // <-- `items` (built with planner rows) is discarded!
}
}When itemsPath is non-empty ("Members"), BuildGroups constructs items using the planner rows (row), but then discards items and keeps the Members collection that SampleObjects.Create(groupType, ...) auto-filled with row: null. Inside SampleObjects.Fill (line 146 of surface/Surface/SampleObjects.cs), when row is null, if (!IsUnset(current, p.PropertyType)) return; sees that Name is already "Runtime item" and skips generating a sample value.
Smallest fix: In the else if (!string.IsNullOrEmpty(itemsPath)) branch of SampleListFiller.BuildGroups, populate the itemsPath collection property on typed using the items list that was already constructed with the planner's rows (just as the IList branch clears and populates l with items).
Version
- Branch / PR:
zateutsch-xaml-previewer(WinUI XAML Previewer for Visual Studio #323) - Commit:
00927d1c049b59160ec4dd26b38f09ab5caed9d9(WinUIXamlPreview.vsixv0.3.15, CI run41725914148)
Activity