我有以下层次结构:
abstract class TicketBase
{
public DateTime PublishedDate { get; set; }
}
class TicketTypeA:TicketBase
{
public string PropertyA { get; set; }
}
class TicketTypeB:TicketBase
{
public string PropertyB { get; set; }
}
在我的虚拟机中,我有一个
List<TicketBase> Tickets
。当用户在我的应用程序上点击一个按钮时,他们希望看到某个属性的以前值的列表,例如:<Button Tag="{x:Type Types:TicketTypeA}"
Command="{Binding ListHistoryCommand}"
CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" />
正如您所看到的,我将我的Tag
属性设置为TicketTypeA并将其作为参数传递给我的命令:
private void ListHistory(object o)
{
if (Tickets.Count == 0)
return;
Type ty = o as Type;
ValueHistory = new ObservableCollection<TicketBase>(GetTicketsOfType(ty).Select(t => t)); // <- Need to return t.PropertyA here, but dynamically
}
IEnumerable<TicketBase> GetTicketsOfType(Type type)
{
if (!typeof(TicketBase).IsAssignableFrom(type))
throw new ArgumentException("Parameter 'type' is not a TicketBase");
return Tickets.Where(p => p.GetType() == type);
}
(ValueHistory
是另一个我在网格上设置为ItemsSource
的集合)
但是我还需要传递属性名称,这样我就可以在网格中仅显示该属性,如下所示:
Published Time | PropertyA
===================================================
09:00 | <value of PropertyA at 09:00>
08:55 | <value of PropertyA at 08:55>
所以问题基本上是如何以最清晰的方式将属性名称作为另一个参数传递到我的命令中?