microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.21k stars 669 forks source link

Unexpected results when nesting binding to ItemsControl #9716

Open GochenRyan opened 1 month ago

GochenRyan commented 1 month ago

Describe the bug

When I use nested models to bind and generate new ItemsControl subclass objects, it is like this:

<local:Model1 x:Name="Mode11111" Text="11111"/>
<local:ItemsModel1 x:Name="ItemsModel1">
    <local:Model1 x:Name="Mode22222" Text="22222"/>
    <local:Model1 x:Name="Mode33333" Text="33333"/>
    <local:ItemsModel1 x:Name="ItemsModel2">
        <local:Model1 x:Name="Mode44444" Text="44444"/>
    </local:ItemsModel1>
</local:ItemsModel1>
<local:Model1 x:Name="Mode55555" Text="55555"/>

--> CustomItemsControl1(itemsControl) --ModelControl1(Control) --ModelControl1(Control) --CustomItemsControl1(itemsControl) ----ModelControl1(Control) --ModelControl1(Control)

This creates an infinite loop. What is the correct way to play?

Steps to reproduce the bug

  1. Clone https://github.com/GochenRyan/WinUISample.git
  2. Checkout commit 50541ea5f2c3a1976c5b29753ccdadfe383cc30c
  3. Set NestedBinding as startup project
  4. Insert a break point at https://github.com/GochenRyan/WinUISample/blob/f1f371e780fe133d1bb6ade33efccf5546ac2a42/WinUISample/NestedBinding/ItemsControl1Selector.cs#L18
  5. Run

Expected behavior

<local:Model1 x:Name="Mode11111" Text="11111"/>
<local:ItemsModel1 x:Name="ItemsModel1">
    <local:Model1 x:Name="Mode22222" Text="22222"/>
    <local:Model1 x:Name="Mode33333" Text="33333"/>
    <local:ItemsModel1 x:Name="ItemsModel2">
        <local:Model1 x:Name="Mode44444" Text="44444"/>
    </local:ItemsModel1>
</local:ItemsModel1>
<local:Model1 x:Name="Mode55555" Text="55555"/>

--> CustomItemsControl1(itemsControl) --ModelControl1(Control) --ModelControl1(Control) --CustomItemsControl1(itemsControl) ----ModelControl1(Control) --ModelControl1(Control)

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.5.3: 1.5.240428000

Windows version

Windows Insider Build (xxxxx)

Additional context

No response

github-actions[bot] commented 1 month ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

GochenRyan commented 1 month ago

I fixed the infinite loop issue (commit c6059d3ee9cd99c11b2d8a488a6bc9aab14b8e21),but it still did not achieve the expected results. It ignored image

GochenRyan commented 1 month ago

I debugged the WinUI source code and found that it was a binding problem with ItemsSource. I fixed it on commit 6551175f5d312bd4860b5c2ec062a2b71409d5a3. I want to know why <local:CustomItemsControl1 ItemsSource="{Binding ItemModes, Mode=OneWay}"/> has problems and how to bind correctly.

GochenRyan commented 3 weeks ago

I shouldn't remove static on commit c6059d3ee9cd99c11b2d8a488a6bc9aab14b8e21. FYI: https://learn.microsoft.com/en-us/windows/uwp/xaml-platform/custom-dependency-properties#collection-type-dependency-properties

GochenRyan commented 3 weeks ago

And WinUI3 do not support a Binding usage for Setter.Value. So I need to set value in code. FYI: https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.setter?view=winrt-26100&redirectedfrom=MSDN#migration-notes

leohu1 commented 4 days ago

So how can we fix this bug, it still happens