I am working on a WPF app where I need to work with comboboxes. I need to add items in the combobox and set the selectedindex in xaml. I have done it successfully but SelectedIndex doesn't appear when I run the application. Here is my code:
XAML:
<ComboBox Grid.Column="0" ItemsSource="{Binding FrequencyList}" SelectedItem="{Binding SelectedFrequencyList, Mode=TwoWay}" SelectedIndex="0" Name="comboBox1" />
<ComboBox Grid.Column="2" ItemsSource="{Binding ModesList}" SelectedItem="{Binding SelectedModesList, Mode=TwoWay}" SelectedIndex="2" Name="comboBox2" />
ViewModel:
public ObservableCollection<string> _FreqList;
_FreqList = new ObservableCollection<string>();
public ObservableCollection<string> _CodecModes;
_CodecModes= new ObservableCollection<string>();
public ViewModel()
{
_FreqList.Add("8 kHz");
_FreqList.Add("11.025 kHz");
_FreqList.Add("12 kHz");
_FreqList.Add("14.7 kHz");
_FreqList.Add("16 kHz");
_FreqList.Add("22.050 kHz");
_FreqList.Add("24 kHz");
_FreqList.Add("29.4 kHz");
_FreqList.Add("32 kHz");
_FreqList.Add("44.100 kHz");
_FreqList.Add("48 kHz");
_FreqList.Add("88.2 kHz");
_FreqList.Add("96 kHz");
_FreqList.Add("undef");
_CodecModes.Add("None");
_CodecModes.Add("A Loop");
_CodecModes.Add("DSP");
_CodecModes.Add("I2S");
}
public ObservableCollection<string> FrequencyList
{
get { return _FreqList; }
set
{
_FreqList = value;
OnPropertyChanged("FrequencyList");
}
}
/// <summary>
/// Selected Frequency List
/// </summary>
private string _selectedFrequencyList;
public string SelectedFrequencyList
{
get { return _selectedFrequencyList; }
set
{
_selectedFrequencyList = value;
int Listvalue = FrequencyList.IndexOf(_selectedFrequencyList);
int ListFinalVal = Listvalue + 1;
SelectedFreq(ListFinalVal);
OnPropertyChanged("SelectedFrequencyList");
}
}
public void SelectedFreq(int Select)
{
int cmd = 0;
int numBytes = 0;
cmd = ((0x8F00 & 0x7F00) | (m_slot & 0xFF));
sendBuf[numBytes++] = Convert.ToByte(Select - 1);
}
public ObservableCollection<string> ModesList
{
get { return _CodecModes; }
set
{
_CodecModes = value;
OnPropertyChanged("ModesList");
}
}
/// <summary>
/// Selected Modes List
/// </summary>
private string _selectedModesList;
public string SelectedModesList
{
get { return _selectedModesList; }
set
{
_selectedModesList = value;
int Modevalue = ModesList.IndexOf(_selectedModesList);
int ModeFinalvalue = Modevalue + 1;
SelectedMode(ModeFinalvalue);
OnPropertyChanged("SelectedModesList");
}
}
public void SelectedMode(int Select)
{
int cmd = 0;
int numBytes = 0;
cmd = ((0x8F00 & 0x7F00) | (m_slot & 0xFF));
sendBuf[numBytes++] = Convert.ToByte(Select - 1);
}
Even though I have set SelectedIndex=1
in my xaml. When I run the app, the combobox has set of items but doesn't display the selected-index. Am I missing something here???