I have the following hierarchy:
abstract class TicketBase
{
public DateTime PublishedDate { get; set; }
}
class TicketTypeA:TicketBase
{
public string PropertyA { get; set; }
}
class TicketTypeB:TicketBase
{
public string PropertyB { get; set; }
}
In my VM I have a List<TicketBase> Tickets
. When a user clicks a button on my app, they want to see a list of previous values of a certain property, e.g.:
<Button Tag="{x:Type Types:TicketTypeA}"
Command="{Binding ListHistoryCommand}"
CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" />
as you can see, I set my Tag
property to TicketTypeA and pass that as parameter to my command:
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
is another collection that I set as ItemsSource
on my grid)
However I need to also pass in the property name too, so that I can display just that property in the grid like so:
Published Time | PropertyA
===================================================
09:00 | <value of PropertyA at 09:00>
08:55 | <value of PropertyA at 08:55>
So the question is basically what is the cleanest way to pass in the property name as another parameter into my command?