dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.86k stars 1.68k forks source link

Fails to bind dictionary to CollectionView on WinUI #19208

Open WisJamison opened 7 months ago

WisJamison commented 7 months ago

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

ghost commented 7 months ago

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.

drasticactions commented 7 months ago

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

WisJamison commented 7 months ago

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

ghost commented 6 months ago

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.

WisJamison commented 6 months ago

Related discussions: Unable to bind CollectionView to Dictionary<string, object>

WisJamison commented 6 months ago

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

SleepyDevelops commented 4 months ago

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; }
WisJamison commented 2 months ago

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.