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

Binding inside ItemsRepeater.DataTemplate broken in 11.1 #16242

Open
TimoObst opened this issue Jul 5, 2024 · 1 comment
Open

Binding inside ItemsRepeater.DataTemplate broken in 11.1 #16242

TimoObst opened this issue Jul 5, 2024 · 1 comment

Comments

@TimoObst
Copy link

TimoObst commented Jul 5, 2024

Describe the bug

Using "{Binding}" inside ItemsRepeater.DataTemplate is binding to the Parent, not to the Item.

To Reproduce

Test case project: TestItemsRepeater.zip

  1. Open and Run the Test case project
  2. The top ItemsRepeater is trying to bind the item to the DataContext of the ItemView and is not working
    2b. The bottom ItemsRepeater is directly using the Bindings on the ItemView and are working
  3. Inspect the ItemView and DataContext will be set to TestItemsRepeater.ViewModels.MainWindowViewModel
    TestItemsRepeater_LGWtfKXFS5

Optional 4. Downgrade Avalonia to 11.0.11 and run. Both ItemsRepeater will now work properly
TestItemsRepeater_UjC1DurtC1

Expected behavior

The DataContext of the ItemView should be set to TestItemsRepeater.ViewModels.ItemViewModel as it used to do in 11.0.11

Avalonia version

11.1.0-rc2

OS

Windows

Additional context

I tested this same setup using ItemControl and that does not seem to have this issue

@TimoObst TimoObst added the bug label Jul 5, 2024
@stevemonaco
Copy link
Contributor

stevemonaco commented Jul 5, 2024

I've tested both versions (11.0.11 and 11.1.0-rc2) and it is a regression. However, there's no need to assign the DataContext to what you're expecting to be...itself?

Original broken ItemsRepeater:

<ItemsRepeater ItemsSource="{Binding Items}">
    <ItemsRepeater.Layout>
        <WrapLayout HorizontalSpacing="20" VerticalSpacing="30" />
    </ItemsRepeater.Layout>

    <ItemsRepeater.ItemTemplate>
        <DataTemplate DataType="vm:ItemViewModel">
            <views:ItemView DataContext="{Binding}" />
        </DataTemplate>
    </ItemsRepeater.ItemTemplate>
</ItemsRepeater>

Change to <views:ItemView /> and it will work as expected.

The root element's concept of what the DataContext is seems to have broken (in only ItemsRepeater, ItemsControl works fine). This may be a problem for a niche case where you need to bind DataContext properties of the Item VM to the root control in the ItemTemplate. In that scenario, you would need a wrapper so it's not the root:

<ItemsRepeater.ItemTemplate>
  <DataTemplate DataType="vm:ItemViewModel">
      <Border>
          <views:ItemView DataContext="{Binding}" SomeProp="{Binding MyProp}" />
      </Border>
  </DataTemplate>
</ItemsRepeater.ItemTemplate>

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

No branches or pull requests

3 participants