Saturday, 24 August 2013

WPF Unable to set Selected Item of a Combo box

WPF Unable to set Selected Item of a Combo box

I have a non-bound Combobox and I want to set its value at run time. I
tried a lot but cannot achieve it. Here's the code :
<ComboBox Background="#FFB7B39D" Grid.Row="1" Height="23"
HorizontalAlignment="Right" Margin="0,26,136,0" Name="cboWellDiameter"
VerticalAlignment="Top" Width="120">
<ComboBoxItem Content="meter" IsSelected="True" />
<ComboBoxItem Content="centimeter" />
</ComboBox>
In code, I am trying with :
//VALUE of sp.wellborediameterField_unit is centimeter
int index =
cboWellDiameter.Items.IndexOf(sp.wellborediameterField_unit); //
Gives -1
Console.WriteLine("Index of well bore dia unit = " +
index.ToString());
cboWellDiameter.SelectedIndex = index;
// cboWellDiameter.SelectedItem = sp.wellborediameterField_unit;
// cboWellDiameter.SelectedValue = sp.wellborediameterField_unit;
SelectedItem & selectedValue has no impact. Why its not even able to find
in Items ? How do I set it ?
Please help me, have several such non-bound and binded combos to set
programmatically.

No comments:

Post a Comment