maui
maui copied to clipboard
Fails to bind dictionary to CollectionView on WinUI
Description
Hi,
I wanna to bind a dictionary to CollectionView. Binding works good on mac.
but failed on WinUI with below error:
Error: Cannot get 'Item' value (type 'Object') from type 'Microsoft.Maui.Controls.Platform.ItemTemplateContext'. BindingExpression: Path='Item' DataItem='Microsoft.Maui.Controls.Platform.ItemTemplateContext'; target element is 'Microsoft.Maui.Controls.Platform.ItemContentControl' (Name='ItemContentControl'); target property is 'FormsDataContext' (type 'Object').
I can also confirm that this binding works fine on BindableLayout.ItemSource of StackLayout.
Steps to Reproduce
- Launch debug on my sample app
- Check error list on XAML Binding Failures
Link to public reproduction project repository
https://github.com/WisJamison/Maui-CollectionView-Sample
Version with bug
7.0.101
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Windows
Affected platform versions
Window 10.0.19052.1000.0
Did you find any workaround?
No response
Relevant log output
No response
Hi @WisJamison. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
.NET 8 is the newest stable version, did you try using that?
Hi @drasticactions Tested on .Net 8, still no luck.
We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.
Related discussions: Unable to bind CollectionView to Dictionary<string, object>
One more thing I have noticed. The Binding of Dictionary works fine when both key & value are primary types. But error occurs for composite types.
Error: Cannot get 'Item' value (type 'Object') from type 'Microsoft.Maui.Controls.Platform.ItemTemplateContext'. BindingExpression: Path='Item' DataItem='Microsoft.Maui.Controls.Platform.ItemTemplateContext'; target element is 'Microsoft.Maui.Controls.Platform.ItemContentControl' (Name='ItemContentControl'); target property is 'FormsDataContext' (type 'Object').
Hi,
for me there is no Error, but the Collectionview will display completely empty.
XAML
<CollectionView ItemsLayout="VerticalList" ItemsSource="{Binding Test}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid RowDefinitions="Auto,Auto">
<Label
Grid.Row="0"
HorizontalOptions="Center"
Text="{Binding Key.Name}" />
<Label
Grid.Row="1"
HorizontalOptions="Center"
Text="{Binding Value}" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Dictionary
public Dictionary<PersonCategory, double> Test { get; set; } = new Dictionary<PersonCategory, double>();
Class
public string Name { get; set; }
public int OrderNr { get; set; }
Hi,
for me there is no Error, but the Collectionview will display completely empty.
XAML
<CollectionView ItemsLayout="VerticalList" ItemsSource="{Binding Test}"> <CollectionView.ItemTemplate> <DataTemplate> <Grid RowDefinitions="Auto,Auto"> <Label Grid.Row="0" HorizontalOptions="Center" Text="{Binding Key.Name}" /> <Label Grid.Row="1" HorizontalOptions="Center" Text="{Binding Value}" /> </Grid> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView>
Dictionary
public Dictionary<PersonCategory, double> Test { get; set; } = new Dictionary<PersonCategory, double>();
Classpublic string Name { get; set; } public int OrderNr { get; set; }
Hi @SleepyDevelops I guess Displaying completely empty means your binding failed. Please take a look at the 'XAML Binding Failures" window to see the failure outputs if you are using Visiual Studio.