Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获得适当的组合框标记_C#_Wpf_Combobox - Fatal编程技术网

C# 如何获得适当的组合框标记

C# 如何获得适当的组合框标记,c#,wpf,combobox,C#,Wpf,Combobox,我试图通过以下方式将标记与ComboBox的值关联: var league = ((ComboBoxItem)this.League.SelectedValue).Tag.ToString(); Console.WriteLine(league); 编译器向我显示无效强制转换异常 我只想按用户获取所选值的关联标记,特别是: (组合框值和标记) -意大利(项目)-10(标签) -法国(项目)-12(标签) 如果用户选择意大利,则在代码中我必须获得“10”。但我不能这么做,我做错了什么 更新(

我试图通过以下方式将标记与ComboBox的值关联:

var league = ((ComboBoxItem)this.League.SelectedValue).Tag.ToString();
Console.WriteLine(league);
编译器向我显示
无效强制转换异常


我只想按用户获取所选值的关联标记,特别是:

(组合框值和标记)

-意大利(项目)-10(标签)
-法国(项目)-12(标签)

如果用户选择意大利,则在代码中我必须获得
“10”
。但我不能这么做,我做错了什么

更新(填充组合):

List obj=JsonConvert.DeserializeObject(responseText);
foreach(obj中的var项目)
{
foreach(国家/地区代码中的var代码)
{
if(代码等于(项目联盟))
{
League.Items.Add(item.Caption);
//每个团队的链接
League.Tag=item.Links.Teams.href;
}
}
}

如果您看到标签是为组合框本身而不是为其单个项目设置的

您可以构建一个字典并将其用作组合框的数据源。使用字典键和值属性指定组合框的值和显示成员

尝试修改组合填充逻辑,如下所示-

        List<RootObject> obj = JsonConvert.DeserializeObject<List<RootObject>>(responseText);
        Dictionary<string, string> comboSource = new Dictionary<string, string>();

        foreach (var item in obj)
        {
            foreach (var code in nation_code)
            {
                if (code.Equals(item.League))
                {
                    comboSource.Add(item.Caption, item.Links.Teams.href);

                }
            }
        }

        League.ValueMember = "Value";
        League.DisplayMember = "Key";
        League.DataSource = comboSource;
对于WPF,我们需要使用不同的属性,即<代码>项目来源, 绑定组合时,
DisplayMemberPath
SelectedValuePath
盒子。上述解决方案适用于win表单


您可以向组合框添加任何类型的对象,它不需要是字符串,只需要覆盖.ToString()

您可以定义一个类:

class League {
  public string Country { get; set; }
  public int Id { get; set; }

  public override string ToString() {
       return Country;
  }
}
然后将这些对象添加到组合框中:

comboBox.Items.Add(new League { Country = "France", Id = 10 });
然后,您可以将组合框的SelectedItem强制转换回您的类:

var selectedLeague = (League)comboBox.SelectedItem;
//access selectedLeague.Country;
//access selectedLeague.Id;

什么是空的
.Tag
?空异常现已修复,抱歉。编译器向我显示:无效的强制转换异常。对不起,我弄错了。那么问题就显而易见了。无法将SelectedValue强制转换为ComboBoxItem。您可以使用调试器确定实例SelectedValue是哪个。好的,那么如何获取所选项目的关联标记?使用
SelectedItem
而不是
SelectedValue
。value成员,显示成员和数据源以红色下划线=>ComboBox不包含ValueMember定义,并且找不到任何接受此参数的方法。该代码适用于Windows窗体应用程序。对于WPF,只需使用以下命令——“League.DisplayMemberPath=“Value”League.SelectedValuePath=“Key”;“对于源,使用ItemSource?是,
ItemsSource
”。由于该问题是针对WPF提出的,因此可能应该对整个答案进行编辑,以适用于WPF而不是WinForms?
comboBox.Items.Add(new League { Country = "France", Id = 10 });
var selectedLeague = (League)comboBox.SelectedItem;
//access selectedLeague.Country;
//access selectedLeague.Id;