C# WP7数据库和强制转换错误

C# WP7数据库和强制转换错误,c#,windows-phone-7,C#,Windows Phone 7,我是c语言开发的新手 我正在研究这个例子 在这个链接中,您可以看到我的代码,我认为我有问题: 当我运行应用程序并点击按钮1时 我有一个无效的强制转换错误,我只在中使用强制转换 Category = (DB.Elements)listPicker.SelectedItem 但我不知道问题出在哪里 最佳富豪 安东尼奥 更多信息 我将其用于listPicker中的插入元素 public Inserimento() { InitializeComponent();

我是c语言开发的新手 我正在研究这个例子 在这个链接中,您可以看到我的代码,我认为我有问题: 当我运行应用程序并点击按钮1时 我有一个无效的强制转换错误,我只在中使用强制转换

    Category = (DB.Elements)listPicker.SelectedItem
但我不知道问题出在哪里 最佳富豪 安东尼奥

更多信息 我将其用于listPicker中的插入元素

 public Inserimento()
    {
        InitializeComponent();
        List<Elenco> source = new List<Elenco>();
        source.Add(new Elenco() { Elemento = "Value1"});
        source.Add(new Elenco() { Elemento = "Value2" });
        source.Add(new Elenco() { Elemento = "Value3" });
        source.Add(new Elenco() { Elemento = "Value4" });
        this.listPicker.ItemsSource = source;
    }
执行Inserimento方法时,listPicker包含Elenco对象的列表。从该listPicker检索项将检索一个Elenco对象。因此:

Category=DB.ElementslistPicker.SelectedItem

实际上应该是这样的:

类别=ElencolistPicker.SelectedItem


没有足够的代码。当您将数据绑定到listPicker时,是否可以显示部件?listPicker.SelectedItem显然不是DB.Elements类型,根据您的代码,它可能是Elementi类型。可以肯定的是,在该行上放置一个断点,然后将鼠标悬停在listPicker.SelectedItem上以查看对象的类型。或者在前面添加此行:System.Diagnostics.Debug.WriteLinelistPicker.SelectedItem.GetType;然后查看输出窗口以查看类型。您将在listPicker中放置一个Elenco列表,因此您不可能希望将其大小写为DB.Elements。把它扔给Elenco,然后用它做任何你需要的事。
namespace Example.ViewModel{
public class Elenco
{  public string Elemento
    {
        get;
        set;
    }

}}