I have one WPF ListBox loaded using LINQ:
lbxCalculosSec.ItemsSource = from p in database.CALCULOS
orderby p.NOMBRECALCULO
select new { ID = p.IDCALCULO, NOMBRE = p.NOMBRECALCULO + " - " + p.DESCRIPCIONCALCULO };
lbxCalculosSec.DisplayMemberPath = "NOMBRE";
lbxCalculosSec.SelectedValuePath = "ID";
The listbox has multiselect = true. The problem is when I try to retrieve all the SelectedValue’s (ID’s) from SelectedItems List.
When I inspect one SelectedItem at runtime, the object type is “<>f__AnonymousType0`2”
I tried using this:
ItemPropertyInfo ID = null;
lbxCalculosSec.SelectedItem.GetType().GetProperty("ID").GetValue(ID as ItemPropertyInfo, null)
But it didn’t work.
I need a solution to access ListBox Selected Values (ID fields).
Thank you very much in advance.
Kind Regards.
GetValueexpects the instance on which the property is defined, in this case it’s theSelectedItem:Edit
If
IDis of some other type, like int, the code should be: