unoplatform / uno

Open-source platform for building cross-platform native Mobile, Web, Desktop and Embedded apps quickly. Create rich, C#/XAML, single-codebase apps from any IDE. Hot Reload included! 90m+ NuGet Downloads!!
https://platform.uno
Apache License 2.0
9.05k stars 734 forks source link

Can not bind Template Parent's Property in ItemsRepeater's ItemTemplate #18819

Open yeahg-dev opened 1 week ago

yeahg-dev commented 1 week ago

What would you like to be added

After Uno 5.5 TemplatedParent updates , It is impossible to bind Templated Parent's Property in ItemsRepeater's ItemTemplate which is DataTemplate

below code works before Uno 5.5 updates.

<Style TargetType="local:CodeBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:CodeBox">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="MatchState">
                                <VisualState x:Name="Unknown"/>
                                <VisualState x:Name="Matched">
                                    <VisualState.Setters>
                                        <Setter Target="DigitsPresenter.Background" Value="Yellow"/>
                                    </VisualState.Setters>
                                </VisualState>
                                <VisualState x:Name="NotMatched">
                                    <VisualState.Setters>
                                        <Setter Target="DigitsPresenter.Background" Value="Pink"/>
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                        <ItemsRepeater Control.IsTemplateFocusTarget="true" x:Name="DigitsPresenter" ItemsSource="{TemplateBinding Digits}"
                            VerticalAlignment="Bottom">
                            <ItemsRepeater.Layout>
                                <StackLayout Orientation="Horizontal" Spacing="8"/>
                            </ItemsRepeater.Layout>
                            <ItemsRepeater.ItemTemplate>
                                <DataTemplate>
                                    <!-- 🚨 This does not work after Uno 5.5: 🚨 -->
                                    <local:CodeBoxDigit IsMatch="{Binding IsMatch, RelativeSource={RelativeSource TemplatedParent}}"
                                        Digit="{Binding .}" />

                                    <!-- 🚨 This also does not work, Besides can't render UI 🚨-->
                                    <!-- <local:CodeBoxDigit IsMatch="{utu:AncestorBinding AncestorType=local:CodeBox, Path=IsMatch}"
                                        Digit="{Binding .}" /> -->
                                </DataTemplate>
                            </ItemsRepeater.ItemTemplate>
                            <ItemsRepeater.RenderTransform>
                                <TranslateTransform/>
                            </ItemsRepeater.RenderTransform> 
                        </ItemsRepeater>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Full Example Code : VisualStateManagerBugImpl-main.zip

Why is this needed

There is a need for using Templated Parent's Property on DataTemplate to keep code simple. If TemplatedParent support in DataTemplate is unavailable because of differences with WinUI, then AncestorBinding should have worked.

Is there any workaround for this issue?

For which platform

Android, iOS

Anything else we need to know?

No response