How can I find value index from ComboBox? I tried this but it always returns -1;
sexCombo.SelectedIndex = sexCombo.Items.IndexOf(teacherInfo["sex"].ToString());
Here how the ComboBox items are added:
<ComboBox x:Name="sexCombo" Margin="5,20,10,0" VerticalAlignment="Top" Width="100" Style="{StaticResource MaterialDesignFloatingHintComboBox}" materialDesign:HintAssist.Hint="الجنس" HorizontalContentAlignment="Left" Height="45" VerticalContentAlignment="Bottom">
<ComboBoxItem Content="ذكر"/>
<ComboBoxItem Content="أنثى"/>
</ComboBox>
The Items collection of the ComboBox contains ComboBoxItems so you need to get the index of the corresponding ComboBoxItem element. Try this:
var comboBoxItem = sexCombo.Items.OfType<ComboBoxItem>().FirstOrDefault(x => x.Content.ToString() == teacherInfo["sex"].ToString());
int index = sexCombo.SelectedIndex = sexCombo.Items.IndexOf(comboBoxItem);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With