0

我正在开发一个需要使用组合框的 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 中设置了。当我运行应用程序时,组合框有一组项目,但不显示选定索引。我在这里错过了什么吗???

4

1 回答 1

3

如果具有初始空值,则设置SelectedIndex=1与您的绑定冲突。SelectedItem

SelectedItem您可以更改to的绑定模式,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" />
于 2012-10-18T04:51:15.423 回答