从组合框中返回两个值

时间:2017-01-08 22:12:37

标签: c# wpf combobox

在我的程序中,我将值存储在列表中。我需要从一个组合框中返回其中两个值。我在互联网上搜索但找不到答案。请给我一些提示。 这是我的代码:

列表中的值:

public class ActualCurrencyBoxData
{
    public string displayValue { get; private set; }
    public decimal currencyCourse { get; set; }       
    public int currencyConverter { get; set; }

    public ActualCurrencyBoxData( string ncurrencyName, string ncurrencyCode, decimal ncurrencyCourse, int ncurrencyConverter)
    {
        displayValue = ncurrencyName +" (" +ncurrencyCode+ ")";
        currencyCourse = ncurrencyCourse;          
        currencyConverter = ncurrencyConverter;           
    }

}

我只能返回一个值(actualCurrencyCourse,我还需要currencyConverter):

List<ActualCurrencyBoxData> checkBoxVal = new List<ActualCurrencyBoxData>();

public void CurrencyBoxLoad(object sender, RoutedEventArgs e)
{
    var comboBox = sender as ComboBox;
    comboBox.ItemsSource = checkBoxVal;
    comboBox.DisplayMemberPath = "displayValue";
    comboBox.SelectedValuePath = "currencyCourse";                                
}

private void Count_Click(object sender, RoutedEventArgs e)
{      
    actualCurrencyCourse =  ActualCurrencyBox.SelectedValue.ToString());  
}

1 个答案:

答案 0 :(得分:2)

您可以使用SelectedItem属性来获取所选项目的对象:

private void Count_Click(object sender, RoutedEventArgs e)
{      
    actualCurrencyCourse = ((ActualCurrencyBoxData)ActualCurrencyBox.SelectedItem).currencyCourse;
    actualCurrencyConverter = ((ActualCurrencyBoxData)ActualCurrencyBox.SelectedItem).currencyConverter;
}