Skip to content

ComboBox SelectedIndex secretly changes in x:Bind #7521

@HO-COOH

Description

@HO-COOH

Describe the bug

I have this runtime type

    [bindable]
    [default_interface]
    runtimeclass MyData
    {
        MyData();
        Int32 Index{ get; set; };
        Windows.Foundation.Collections.IObservableVector<String> Versions{ get; };
    }

And the code behind

    struct MyData : MyDataT<MyData>
    {
        MyData() = default;

        int Index() 
        { 
            return m_index; 
        }

        void Index(int index) 
        { 
            m_index = index; 
        }

        winrt::Windows::Foundation::Collections::IObservableVector<winrt::hstring> Versions() { return m_versions; }
        winrt::Windows::Foundation::Collections::IObservableVector<winrt::hstring> m_versions = winrt::single_threaded_observable_vector<winrt::hstring>();
        int m_index = 0;
    };

I have a ListView in MainPage.xaml using ItemTemplate

        <ListView ItemsSource="{x:Bind data}">
            <ListView.ItemTemplate>
                <DataTemplate x:Name="template" x:DataType="local:MyData">
                    <ComboBox x:Name="box" SelectedIndex="{x:Bind Index}" ItemsSource="{x:Bind Versions}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

MainPage.idl

    [default_interface]
    runtimeclass MainPage : Windows.UI.Xaml.Controls.Page
    {
        MainPage();
        Windows.Foundation.Collections.IObservableVector<MyData> data{ get; };
    }

Fill the data in the code-behind

    MainPage::MainPage()
    {
        InitializeComponent();
        for (int i = 0; i < 1000; ++i)
        {
            MyData newData;
            newData.Versions().Append(L"A");
            newData.Versions().Append(L"B");
            newData.Versions().Append(L"C");
            newData.Versions().Append(L"D");
            m_data.Append(newData);
        }
    }

Steps to reproduce the bug

  1. Launch the app, and scroll a little bit down. You can see most of those ComboBox are not populated. Put a break point in the Index setter in the MyData.cpp, you can see the parameters are given a value of -1, while the m_index is actually initialized to 0

Expected behavior

No response

Screenshots

image

NuGet package version

Microsoft.UI.Xaml 2.8.0

Windows app type

  • UWP
  • Win32

Device form factor

Desktop

Windows version

Windows 11 (22H2): Build 22621

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions