maui icon indicating copy to clipboard operation
maui copied to clipboard

Fails to bind dictionary to CollectionView on WinUI

Open WisJamison opened this issue 1 year ago • 6 comments

Description

Hi,

I wanna to bind a dictionary to CollectionView. Binding works good on mac. image

but failed on WinUI with below error: image

image 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

  1. Launch debug on my sample app
  2. 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

WisJamison avatar Dec 05 '23 05:12 WisJamison

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.

ghost avatar Dec 05 '23 06:12 ghost

.NET 8 is the newest stable version, did you try using that?

drasticactions avatar Dec 05 '23 06:12 drasticactions

Hi @drasticactions Tested on .Net 8, still no luck.

WisJamison avatar Dec 05 '23 12:12 WisJamison

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.

ghost avatar Dec 21 '23 19:12 ghost

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').

WisJamison avatar Dec 26 '23 14:12 WisJamison

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; }

SleepyDevelops avatar Mar 19 '24 21:03 SleepyDevelops

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 @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.

WisJamison avatar May 07 '24 02:05 WisJamison