如何在C#中将listBox选中的项目作为KeyValuePair <string,string>?

jM2*_*.me 2 c# listbox selecteditem bindinglist key-value

ListBox 对象绑定 BindingList<KeyValuePair<string, string>>

在SelectionChanged事件中,我需要将所选项目作为 KeyValuePair<string, string>

以下代码给出错误,因为KeyValuePair不能用作引用类型.

KeyValuePair<string, string> selectedProperty = listProperties.SelectedItem as KeyValuePair<string, string>;
Run Code Online (Sandbox Code Playgroud)

什么是好的解决方法?

Gra*_*mas 8

尝试使用直接转换而不是as:

var selectedProperty = (KeyValuePair<string, string>)listProperties.SelectedItem;
Run Code Online (Sandbox Code Playgroud)