在wpf中获取selecteditem时出错 [英] error in get selecteditem in wpf

查看:121
本文介绍了在wpf中获取selecteditem时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿家伙

i有wpf数据网格以下列



hey guys
i have wpf Data Grid with below columns

IDStuff(int), Title(string), Type(string),  InitialBalance(int)





及以下课程



and below class

class stuffdt
{

    public int IDStuff { set; get; }
    public string Title { set; get; }
    public string Type { set; get; }
    public int InitialBalance { set; get; }


}







现在我写这段代码








now i write this code


private void dtstuff_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  {
      List<cellprod> cp = new List<cellprod>();
      stuffdt sd =(stuffdt)this.dtstuff.SelectedItem;
  }





但我有这个错误





but i have this error

Unable to cast object of type '<>f__AnonymousType1`4[System.Int32,System.String,System.String,System.Int32]' to type 'stuffdt'.

推荐答案

看看类似的问题:

读取WPF DataGrid中的单元格值 [ ^ ]

如何在wpf中获取datagrid特定的单元格值? [ ^ ]
Have a look at similar questions:
Reading a cell value in a WPF DataGrid[^]
how to get datagrid particular cell value in wpf ?[^]


你不能强制转换动态对象。两者必须完全相同才能正确映射。



我建议你尝试使用listbox itemsource来检索你想要的记录。一些东西



喜欢:



you cannot typecast dynamic objects. the two have to be identical for them to be mapped correctly.

i'd suggest you try using the listbox itemsource to retrieve the records that you want. something

like:

private void dtstuff_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
...

stuffdt sd = {DataGridItemSource}[dtstuff.SelectedIndex]; 

//assuming the datagriditemsource is a list of type stuffdt

...
}


这篇关于在wpf中获取selecteditem时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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