unoplatform / uno.toolkit.ui

A set of custom controls for the WinUI and the Uno Platform not offered out of the box by WinUI, such as Card, TabBar, NavigationBar, etc.
https://platform.uno/
MIT License
82 stars 27 forks source link

[ItemsRepeaterExtensions.SelectedItem] Only works if it's put below the Itemsource #918

Open Marc-Antoine-Soucy opened 11 months ago

Marc-Antoine-Soucy commented 11 months ago

Current behavior

image

<muxc:ItemsRepeater utu:ItemsRepeaterExtensions.SelectedItem="{Binding Test, Mode=TwoWay}" utu:ItemsRepeaterExtensions.SelectionMode="SingleOrNone" ItemsSource="{Binding Tests}">

The selected item will not be set if it's put like this.

Expected behavior

It should be documented that it only works if you set the itemssource first or it should work in any order

How to reproduce it (as minimally and precisely as possible)

<Grid>
    <utu:AutoLayout Spacing="16">
        <TextBlock Style="{StaticResource TitleSmall}"
                   Text="If you put ItemsSource after the extensions"
                   TextWrapping="Wrap" />
        <muxc:ItemsRepeater utu:ItemsRepeaterExtensions.SelectedItem="{Binding Test, Mode=TwoWay}"
                            utu:ItemsRepeaterExtensions.SelectionMode="SingleOrNone"
                            ItemsSource="{Binding Tests}">

            <muxc:ItemsRepeater.ItemTemplate>
                <DataTemplate>
                    <utu:Chip Background="{ThemeResource SurfaceBrush}"
                              Content="{Binding}"
                              Foreground="{ThemeResource OnSurfaceVariantBrush}"
                              HorizontalAlignment="Stretch"
                              BorderThickness="1"
                              Style="{StaticResource MaterialChipStyle}" />
                </DataTemplate>
            </muxc:ItemsRepeater.ItemTemplate>
        </muxc:ItemsRepeater>

        <TextBlock Style="{StaticResource TitleSmall}"
                   Text="If you put ItemsSource before the extensions"
                   TextWrapping="Wrap" />

        <muxc:ItemsRepeater ItemsSource="{Binding Tests}"
                            utu:ItemsRepeaterExtensions.SelectedItem="{Binding Test, Mode=TwoWay}"
                            utu:ItemsRepeaterExtensions.SelectionMode="SingleOrNone">

            <muxc:ItemsRepeater.ItemTemplate>
                <DataTemplate>
                    <utu:Chip Background="{ThemeResource SurfaceBrush}"
                              Content="{Binding}"
                              Foreground="{ThemeResource OnSurfaceVariantBrush}"
                              HorizontalAlignment="Stretch"
                              BorderThickness="1"
                              Style="{StaticResource MaterialChipStyle}" />
                </DataTemplate>
            </muxc:ItemsRepeater.ItemTemplate>
        </muxc:ItemsRepeater>
    </utu:AutoLayout>

</Grid>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        this.DataContext = this;
    }

    public int[] Tests { get; }  = { 1, 2, 3, 4 };

    public int Test { get; set; } = 2;
}

Nuget Package:

Package Version(s):

Affected platform(s): It happens on every platform I tested

IDE:

Relevant plugins:

Anything else we need to know?

This happens because the collection is still empty when the extensions tries to set the selected item.

kazo0 commented 11 months ago

fyi @Xiaoy312