我正在开发一个需要使用组合框的 WPF 应用程序。我需要在组合框中添加项目并在 xaml 中设置 selectedindex。我已成功完成,但运行应用程序时未出现 SelectedIndex。这是我的代码:
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" />
视图模型:
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);
}
即使我已经SelectedIndex=1
在我的 xaml.xml 中设置了。当我运行应用程序时,组合框有一组项目,但不显示选定索引。我在这里错过了什么吗???