【问题标题】:How to get listBox selected item as KeyValuePair<string, string> in C#?如何在 C# 中将列表框选定项作为 KeyValuePair<string, string>?
【发布时间】:2011-06-19 19:07:52
【问题描述】:

ListBox 对象与BindingList&lt;KeyValuePair&lt;string, string&gt;&gt; 绑定

在 SelectionChanged 事件中,我需要将所选项目设为 KeyValuePair&lt;string, string&gt;

以下代码会出错,因为 KeyValuePair 不能用作引用类型。

KeyValuePair<string, string> selectedProperty = listProperties.SelectedItem as KeyValuePair<string, string>;

有什么好的解决方法?

【问题讨论】:

    标签: c# listbox selecteditem bindinglist key-value


    【解决方案1】:

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

    var selectedProperty = (KeyValuePair<string, string>)listProperties.SelectedItem;
    

    【讨论】:

    • 这是一个很好的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2017-05-21
    • 2019-09-17
    • 1970-01-01
    • 2013-01-10
    • 2019-09-19
    相关资源
    最近更新 更多