【发布时间】:2011-07-03 13:51:36
【问题描述】:
我有以下层次结构:
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。当用户单击我的应用程序上的按钮时,他们希望查看某个属性的 previous 值列表,例如:
<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 的另一个集合)
但是我还需要传入 property 名称,这样我就可以像这样在网格中显示该属性:
Published Time | PropertyA
===================================================
09:00 | <value of PropertyA at 09:00>
08:55 | <value of PropertyA at 08:55>
所以问题基本上是将属性名称作为另一个参数传递给我的命令的最干净的方法是什么?
【问题讨论】:
标签: wpf parameters command