我正在制作一个UserControl使用ListBox. 在某些时候,我需要在ListBox. 我会走的“正常”方式是:
var selected = (CustomObject)listBox.SelectedItem;
var str = selected.PropertyShowingInListBox;
但由于某些原因,UserControl我无法投射SelectedItem. 所以我的下一个机会是使用SelectedItemasobject和DisplayMemberPath. 像这样:
var selected = listBox.SelectedItem;
var str = selected.GetType().GetProperty(listBox.DisplayMemberPath).GetValue(selected, null).ToString();
但这不是最好的。我有什么方法可以使用吗?