Skip to content

[Bug]: XAML Previewer sample data for CollectionViewSource with ItemsPath ignores generated rows when item properties have C# initializers #344

Open

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

  1. Define a group class with an ItemsPath collection 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; } = [];
    }
  2. Bind a ListView through a CollectionViewSource with ItemsPath="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}"/>
  3. 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 of IList-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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions