1

我已经为ComboBox. 选择ComboBox列表正确显示,但是当用户选择一个值时,我希望能够检索 3 个值中的任何一个(sDirectoryAliassDirectorybFileName)。这是我的代码:

public class ComboBoxItem_DirectoryToLoad
{
    public string sDirectoryAlias { get; set; }
    public string sDirectory { get; set; }
    public bool bFileName { get; set; }
}

private void BuildDirectoryComboBox()
{
    var dataSource = new List<ComboBoxItem_DirectoryToLoad>();
    foreach (KeyValuePair<string, string> pair in cls_global_variables.dictDirectoriesToLoad)
    {
        string[] sTemp = pair.Value.Split('|');           
        dataSource.Add(new ComboBoxItem_DirectoryToLoad() { sDirectoryAlias = sTemp[0], sDirectory = sTemp[1], bFileName = Convert.ToBoolean(sTemp[2]) });
    }

    this.cb_directories_to_load.DataSource = dataSource;
    this.cb_directories_to_load.DisplayMember = "sDirectoryAlias";
}

private void cb_directories_to_load_SelectedIndexChanged(object sender, EventArgs e)
{
    string s = this.cb_directories_to_load.SelectedItem["sDirectory"];
}

所以在这里不起作用的部分是:

string s = this.cb_directories_to_load.SelectedItem["sDirectory"];

在 上SelectedIndexChanged,我希望能够分配s给 3 个类属性中的任何一个。

4

0 回答 0