如何检索组合框的选定值(不是文本)

问题描述 投票:-1回答:1

我一直在做一个小应用程序在AD中做一些用户帐户创建,但我似乎被我的ComboBox卡住了。

目前在我的项目中有一种方法可以将项添加到特定的ComboBox:

private void AddAccessBox(string name, string value)
    {
        ComboboxItem newitem = new ComboboxItem();
        newitem.Text = name;
        newitem.Value = value;
        accessLevelBox.Items.Add(newitem);
    }

要将项添加到ComboBox:

AddAccessBox("Standard User", "SSLVPN,anothergroup,andanother");

由于一些不道德的原因,我无法弄清楚如何获得所选项的值,SelectedValue和SelectedItem都返回“标准用户”。

enter image description here

我确信我有一些小小的东西,我很遗憾,任何帮助都会非常感激。

c# wpf visual-studio combobox
1个回答
1
投票

您已将ComboboxItem实例分配给ComboBox的Items集合,因此SelectedItem(或SelectedValue)返回ComboboxItem:

ComboboxItem item = accessLevelCombobox.SelectedItem as ComboboxItem;

if (item != null && item.Value == "SSLVPN,anothergroup,andanother")
{
}
© www.soinside.com 2019 - 2024. All rights reserved.