Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default selected index for combo-box crashes my UWP App

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?

like image 283
sohel14_cse_ju Avatar asked Nov 16 '25 22:11

sohel14_cse_ju


1 Answers

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

like image 71
Vincent Avatar answered Nov 19 '25 12:11

Vincent