使用字典绑定组合框作为数据源,给出'System.Reflection.TargetException'的例外

时间:2015-04-21 09:30:51

标签: c# xaml dictionary combobox data-binding

大家好我想用我的字典填充我的动态组合框。当我像这样运行我的代码时:

C#Model

class QuestionModel
{
    public QuestionModel(Dictionary<string, string> Answers)
    {
        answers=Answers;
    }
    public Dictionary<string, string> answers { get; set; }
}

C#Dictionary

Dictionary<string, string> AnsStore = new Dictionary<string, string>();
AnsStore.Add("1", "Chess");
AnsStore.Add("2", "Darts");

questionCollection.Add(new QuestionModel(AnsStore));

的Xaml

<ComboBox Height="40" VerticalAlignment="Stretch" x:Name="QuestioncomboBox" Grid.Column="1" FontSize="25" ItemsSource="{Binding Path=answers}" />

我的组合框的输出是:

  1,国际象棋   2,飞镖

所以为了让我的组合框只输出值而不是键

  

象棋
  飞镖

我将我的Xaml代码更改为:

Xaml

<ComboBox Height="40" VerticalAlignment="Stretch" x:Name="QuestioncomboBox" Grid.Column="1" FontSize="25" ItemsSource="{Binding Path=answers}" SelectedValuePath="Key" DisplayMemberPath="Value" />

但是当我运行我的应用程序并单击组合框时,我得到了这个例外。

  

mscorlib.dll中出现'System.Reflection.TargetException'类型的异常,并且在托管/本机边界之前未处理   附加信息:对象与目标类型不匹配。

我做错了什么以及如何更正我的代码以解决此问题?

0 个答案:

没有答案