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

ElementName bindings don't work inside ItemsRepeater DataTemplate #560

Closed
JohnnyWestlake opened this issue Apr 15, 2019 · 27 comments
Closed
Labels
area-Binding area-ItemsRepeater bug Something isn't working closed-Fixed Described behavior has been fixed. fix-released The fix has been in a release (experimental, preview, stable, or servicing). team-Controls Issue for the Controls team team-Markup Issue for the Markup team wct
Milestone

Comments

@JohnnyWestlake
Copy link

JohnnyWestlake commented Apr 15, 2019

Describe the bug
As described, using ElementName bindings inside the datatemplate of an Items Repeater appears to fail. The same template inside anything that inherits ItemsControl works fine.

Steps to reproduce the bug

Steps to reproduce the behavior:

  1. Create an ItemsRepeater
  2. Inside it's DataTemplate, use an elementname binding to something outside of the ItemsRepeaters VisualTree.
  3. Observe infinite nothingness

Expected behavior
It works.

Version Info
O.S. 18362.53
MinVersion SDK: 17134
MaxVersion SDK: 17763

NuGet package version:
2.1.190405004

@JohnnyWestlake
Copy link
Author

JohnnyWestlake commented Jan 21, 2021

Is there any follow up on this? Almost 2 years later and it seems like a pretty big thing to be missing, especially if we want ItemsRepeater to replace ListView/GridView, where it works fine.

@crodeheaver
Copy link

I'm also running into the same issue. Are there any work arounds for the issue?

@LanceMcCarthy
Copy link

@jevansaks I can confirm this bug is still present. I've tried both ElementName and RelativeSource bindings, nothing seems to be able to change the DataContext of that binding as long as it's inside the DataTemplate.

@jevansaks
Copy link
Member

I'm sorry to hear that. @RealTommyKlein @ranjeshj any ideas?

@evelynwu-msft evelynwu-msft added area-Binding team-Markup Issue for the Markup team labels Jan 9, 2023
@agneszitte
Copy link
Contributor

agneszitte commented Mar 3, 2023

I'm also running into the same issue even with the latest versions.
(cc @michael-hawker)

@michael-hawker
Copy link
Collaborator

michael-hawker commented Jun 21, 2023

This is a pain see folks hit trying to implement MVVM patterns. This effects both WinUI 2 and WinUI 3.

Someone recently created a nice isolated repro of the ItemsControl/ItemsRepeater difference here on one of our bugs, thanks to @leoshusar:

<ItemsControl ItemsSource="{x:Bind ViewModel.Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate x:DataType="x:String">
            <ComboBox Name="cmbTest1">
                <ComboBoxItem Content="TextBlock" Tag="TextBlock" />
                <ComboBoxItem Content="HyperLink" Tag="HyperLink" />
                <i:Interaction.Behaviors>
                    <ic:EventTriggerBehavior EventName="SelectionChanged">
                        <ic:EventTriggerBehavior.Actions>
                            <ic:InvokeCommandAction Command="{Binding ElementName=PageRoot, Path=ViewModel.Test1Command}" CommandParameter="{Binding ElementName=cmbTest1}" />
                        </ic:EventTriggerBehavior.Actions>
                    </ic:EventTriggerBehavior>
                </i:Interaction.Behaviors>
            </ComboBox>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

<ItemsRepeater Grid.Column="1" ItemsSource="{x:Bind ViewModel.Items}">
    <ItemsRepeater.ItemTemplate>
        <DataTemplate x:DataType="x:String">
            <ComboBox Name="cmbTest2">
                <ComboBoxItem Content="TextBlock" Tag="TextBlock" />
                <ComboBoxItem Content="HyperLink" Tag="HyperLink" />
                <i:Interaction.Behaviors>
                    <ic:EventTriggerBehavior EventName="SelectionChanged">
                        <ic:EventTriggerBehavior.Actions>
                            <ic:InvokeCommandAction Command="{Binding ElementName=PageRoot, Path=ViewModel.Test2Command}" CommandParameter="{Binding ElementName=cmbTest2}" />
                        </ic:EventTriggerBehavior.Actions>
                    </ic:EventTriggerBehavior>
                </i:Interaction.Behaviors>
            </ComboBox>
        </DataTemplate>
    </ItemsRepeater.ItemTemplate>
</ItemsRepeater>
Recording.2023-06-19.011441.mp4

App22.zip

FYI @bpulliam

@AndrewKeepCoding
Copy link
Contributor

Here's a sample app project with a workaround that works for me.

If you want to see it in action, here's a video.

@evelynwu-msft evelynwu-msft added the bug Something isn't working label Jul 10, 2023
@LeonSpors
Copy link

Any ETA for this issue?

@ranjeshj
Copy link
Contributor

Not sure what ItemsControl is doing to make this work. Here is a simpler repro

    <StackPanel>
        <Button x:Name="button" Content="MyButton" />
        <ItemsRepeater x:Name="repeater">
            <ItemsRepeater.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding ElementName=button, Path=Content}" />
                </DataTemplate>
            </ItemsRepeater.ItemTemplate>
        </ItemsRepeater>
    </StackPanel>

    repeater.ItemsSource = Enumerable.Range(0, 10);

There is a small typo in the repro steps above - cmbTest2 does not exist.

@JohnnyWestlake
Copy link
Author

JohnnyWestlake commented Aug 30, 2023

I would presume it is a XAML namescope issue as ElementName requires the element to be in the same namescope, but as we can't debug the namescoping externally someone MS related will have to track it down. But given how "non-traditional" ItemsRepeater layouts are handled compared to normal Xaml ItemsControls I would imagine there's an obvious break point. It could be that elements are namescoped to the RecyclePool for example.

@ghost1372
Copy link
Contributor

@bpulliam any news? i can not use new control ItemsView because of this bug.

@michael-hawker
Copy link
Collaborator

Here's a sample app project with a workaround that works for me.

I wouldn't call adding Loading events to set properties in code-behind as a workaround to a binding issue; sure it may work, but it's so much extra code for each UIElement and property compared to the original binding.

x:Bind/Binding is just broken in so many of these scope scenarios which adds so much friction in developing with XAML (see #2508). If a bit of focus could be spent in this space, it could make the platform a dream to work with.

@ghost1372
Copy link
Contributor

@duncanmacmichael any new for this?

@AndrewKeepCoding
Copy link
Contributor

I wouldn't call adding Loading events to set properties in code-behind as a workaround to a binding issue; sure it may work, but it's so much extra code for each UIElement and property compared to the original binding.

@michael-hawker
I totally agree with you. It's not clean and it's too much extra code for a simple binding. I just had a case that needed to make it work.
This issue and #2508 are getting very old and I'm concerned if they will ever get fixed.

@duncanmacmichael
Copy link
Member

duncanmacmichael commented Oct 17, 2023

@duncanmacmichael any new for this?

I'm honestly not sure since engineering handles bugs directly and I'm a PM, but it looks like @ranjeshj is working on this so we can ask him for an update. :)

@ranjeshj
Copy link
Contributor

@RealTommyKlein helped narrow this down to this code -

IFC_RETURN(panel->get_IsItemsHost(&fIsItemsHost));
ItemsRepeater is not an ItemsHost so it is being skipped from this check.

@ghost1372
Copy link
Contributor

@ranjeshj any update?

@llongley llongley added the closed-Fixed Described behavior has been fixed. label Feb 15, 2024
@ghost1372
Copy link
Contributor

@llongley fixed in v1.5 or v1.4.5?

@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Feb 15, 2024
@codendone codendone added this to the WinAppSDK 1.5 milestone Feb 15, 2024
@codendone
Copy link
Contributor

codendone commented Feb 15, 2024

@ghost1372 This is fixed in the upcoming release of v1.5.

@DominicMaas
Copy link

This doesn't appear to be fixed? (1.5.240404000), anyone else here still encountering the issue?

@ranjeshj
Copy link
Contributor

The simple repro I had works on latest 1.5 release. #560 (comment).

@DominicMaas is your repro different? can you file a new issue with your repro (preferably small/standalone)

@ghost1372
Copy link
Contributor

The simple repro I had works on latest 1.5 release. #560 (comment).

@DominicMaas is your repro different? can you file a new issue with your repro (preferably small/standalone)

Please test with this repro
#560 (comment)

@AathifMahir
Copy link

Still Facing this Issue on Windows App SDK v1.5.240404000

@VigneshVoid
Copy link

Same here still facing the issue.

@AndrewKeepCoding
Copy link
Contributor

I run my repro project with all v1.5.x available versions but none of them worked.

@codendone codendone reopened this Jun 25, 2024
@codendone codendone removed the needs-triage Issue needs to be triaged by the area owners label Jun 25, 2024
@llongley llongley modified the milestones: WinAppSDK 1.5, WinAppSDK 1.6 Jul 16, 2024
@leohu1
Copy link

leohu1 commented Jul 26, 2024

@RealTommyKlein helped narrow this down to this code -

IFC_RETURN(panel->get_IsItemsHost(&fIsItemsHost));

ItemsRepeater is not an ItemsHost so it is being skipped from this check.

The same thing happens to ItemsView, I believe.

@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Jul 26, 2024
@ghost1372
Copy link
Contributor

it seems that issue fixed in v1.6-exp2
vvv

@codendone codendone added fix-released The fix has been in a release (experimental, preview, stable, or servicing). and removed needs-triage Issue needs to be triaged by the area owners labels Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Binding area-ItemsRepeater bug Something isn't working closed-Fixed Described behavior has been fixed. fix-released The fix has been in a release (experimental, preview, stable, or servicing). team-Controls Issue for the Controls team team-Markup Issue for the Markup team wct
Projects
None yet
Development

No branches or pull requests