2012-10-18 49 views
0

我正在WPF應用程序,我需要使用組合框。我需要在組合框中添加項目,並將選定的索引設置爲xaml。我已經成功完成了,但是當我運行應用程序時,SelectedIndex沒有出現。這裏是我的代碼:Combobox不顯示SelectedIndex WPF中啓動

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); 
    } 

即使我在XAML中已設置SelectedIndex=1。當我運行應用程序時,組合框具有一組項目,但不顯示選定索引。我在這裏錯過了什麼?

回答

2

如果SelectedItem具有初始空值,設置SelectedIndex=1與您的綁定衝突。

可以chnage爲SelectedItem結合模式OneWayToSource,這樣在視圖模型中的價值將不會被用於設置所選項目:

<ComboBox Grid.Column="0" ItemsSource="{Binding FrequencyList}" SelectedItem="{Binding SelectedFrequencyList, Mode=OneWayToSource}" SelectedIndex="1" Name="comboBox1" /> 

另外,您可以在視圖模型初始化SelectedFrequencyList到所需的頻率:

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"); 

    SelectedFrequencyList = _FreqList[1]; 
} 

在這種情況下,你並不需要設置爲組合框在所有選定的指標:

<ComboBox Grid.Column="0" ItemsSource="{Binding FrequencyList}" SelectedItem="{Binding SelectedFrequencyList, Mode=TwoWay}" Name="comboBox1" /> 
+0

那麼這是一個理想的解決方案:)非常感謝:) – StonedJesus