WPF ComboBox - 获取Newly SelectedItem

时间:2014-06-25 17:51:16

标签: c# wpf combobox selectionchanged

在WPF中,使用JComboBox并具有SelectionChanged =" myComboBox_SelectionChanged" xaml中的字段,如何获取所选项目的字符串?下面的代码显示以前所选项目的字符串。

private void myComboBox_SelectionChanged(object sender, SelectionChangedEventArgs args)
{
     var comboBox = (sender as ComboBox);
     Console.WriteLine(comboBox.Text);  //prints out PREVIOUSLY selected item
}

1 个答案:

答案 0 :(得分:0)

尝试事件args上的AddedItems属性。这将获得一个包含所选项目的列表:

    private void myComboBox_SelectionChanged(object sender, SelectionChangedEventArgs args)
    {
        IList items = args.AddedItems;
        object firstSelecteditem = items[0];
    }