Default selected index for combo-box crashes my UWP App

2

Why the default selected index doesn't work? It crashes with platform exception :

<ComboBox x:Name="dCmbControl" x:Uid="dCmbControl" SelectionChanged="ComboBox_SelectionChanged" SelectedIndex="0" ItemsSource="{x:Bind abc}"/>

RumTime Error :

Exception thrown at 0x00007FFDEF7F7788 (KernelBase.dll) in abc.exe: 0x40080201: WinRT originate error (parameters: 0x00000000802B000A, 0x000000000000003E, 0x00000066746FBB90).
Exception thrown at 0x00007FFDEF7F7788 in abc.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x00000066746FC0E0. HRESULT:0x802B000A The text associated with this error code could not be found.

What am i doing wrong?

uwp
winrt-xaml
uwp-xaml
c++-cx

2 Answers

2

With the SelectedIndex set to 0, the ComboBox is trying to access its first child to select it. When the XAML is loading, the control is built and SelectedIndex is initialized to 0. This will update the related SelectedItem property but since, there is still no items available at this point, an exception is raised.

Unfortunately, the raised exception does not contains any details about this (it is the same in C#).

You can either set the value to -1 in the XAML and update it to what you want once the combobox is loaded (either through the code behind or through the binding).

answered on Stack Overflow Nov 8, 2017 by Vincent
1

I had a similar problem. What I did was manually add the items from the list (The same list as i used in the x:bind) to the combobox just before i needed to set it. This way it is always loaded when you need it.

answered on Stack Overflow May 2, 2018 by Marijn Donders

User contributions licensed under CC BY-SA 3.0