如何获取在填充Dictionary的ComboBox中SelectedValue的值 [英] How to get value of SelectedValue in ComboBox filled with Dictionary

查看:371
本文介绍了如何获取在填充Dictionary的ComboBox中SelectedValue的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有这样的字典:

  var dictionary = new Dictionary< int,int& {{0,100},{1,202},{2,309},}; 

等等很多值。字典绑定到comboBox像这样:

  comboBox1.ItemsSource = dictionary; 
comboBox1.DisplayMemberPath =Value;

我想知道如何comboBox.Text只能手动输入值和此代码:

  string value = comboBox1.SelectedValue.ToString 

返回值如[1,202],而我需要清除int TValue202。我无法找到类似的问题,所以我在那里问,希望答案可能对别人有用。

解决方案

看起来您必须将 SelectedValue 变成 KeyValuePair< int,int>

  string value =((KeyValuePair< int,int>)comboBox1.SelectedValue).Value.ToString();但是,你应该放一个brak点,并检查什么类型 SelectedValue 

真的是。



我假设它是 KeyValuePair< int,int> 字典< int,int> 以及因为 SelectedValue.ToString()的输出字符串 [1,202]


We have dictionary like this:

var dictionary = new Dictionary<int, int> { { 0, 100 }, { 1, 202 }, { 2, 309 }, };

and so on a lot of values. dictionary binded to comboBox like this:

comboBox1.ItemsSource = dictionary;
comboBox1.DisplayMemberPath = "Value";

I'm wonder how can I get selectedvalue of this comboBox, if comboBox.Text works only for manually inputted values and this code:

string value = comboBox1.SelectedValue.ToString();

return value like [1, 202], while I need clear int TValue "202". I'm unable to find similar question so I ask it there and hope that the answer may be useful for someone else.

解决方案

Looks like you have to cast SelectedValue into KeyValuePair<int, int>:

string value = ((KeyValuePair<int, int>)comboBox1.SelectedValue).Value.ToString();

However, you should put a brakepoint there and check what type SelectedValue really is.

I assume it's KeyValuePair<int, int> because your source collection is Dictionary<int, int> and because of output string for SelectedValue.ToString() which is [1, 202].

这篇关于如何获取在填充Dictionary的ComboBox中SelectedValue的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆