Maui.Markup
Maui.Markup copied to clipboard
[Bug] BindableLayout is not working - Making use of the ItemTemplate markup method
Description
BindableLayout is not working - Making use of the ItemTemplate markup method.
Stack Trace
Link to Reproduction Sample
https://github.com/egvijayanand/markup-issue-89
Steps to Reproduce
- Clone the repository
- Navigate to the src folder
- Open the LayoutApp solution
- Set Windows as the platform target, then build and run the project
- For testing, have provided both XAML and C# versions, switch to the appropriate start page in App.xaml.cs
Expected Behavior
A repeater kind of view that generates the view from the defined template for each of the items in the list.
Actual Behavior
No output. The screen is blank.
Basic Information
- Version with issue: 1.0.1
- Last known good version: NA
- IDE: VS2022 17.3.0 Preview 4.0
- Platform Target Frameworks:
- iOS:
- Android:
- UWP/WinUI: net6.0-windows10.0.19041.0 - running on Windows 11
- Android Support Library Version:
- NuGet Packages:
- Affected Devices:
Workaround
XAML version works as expected.
Reproduction imagery
Thanks Vijay! Could you add a link to your reproduction sample?
Working on, just give me a few more mins.
Thanks for your patience. Here's the simulation source repo.
https://github.com/egvijayanand/markup-issue-89
Another observation, there're only 4 items in the source collection, but it creates 5 blocks.
Hey Vijay!
This is user error. You need to Bind to your ViewModel's Items property.
.ItemSource() just sets a static value for the BindableLayout.ItemsSourceProperty.
Interestingly, because .ItemSource() accepts an IEnumerable, it accepted your string, nameof(ManViewModel.Items), because it has an implicit cast to IEnumerable<char>.
Another observation, there're only 4 items in the source collection, but it creates 5 blocks
This happened because nameof(MainViewModel.Items) has 5 characters and thus BindableLayout created 5 DataTemplates:
IEnumerable<char> source = nameof(MainViewModel.Items); // source = "Items"
Debug.WriteLine(source.Count()); // Prints 5
Provided Sample Code
.ItemsSource(nameof(MainViewModel.Items))
Fixed Code
.Bind(BindableLayout.ItemsSourceProperty, nameof(MainViewModel.Items))
I don't plan to change anything in CommunityToolkit.Maui.Markup because string is a valid input for BindableLayout.ItemSource
Yes, @brminnick.
Have seen this issue in another project in the recent past, this BindableLayout's ItemsSource() method is an exception as it expects the collection itself rather than the property name.
Thanks for your time in analyzing it.