I have a view model that currently looks like below. The TCm type is a domain model type, ie TelephoneNumber. And there are subclassed PcmDetailVms (ie, TelephoneNumberPcmDetailVm). I want to use these properties for a DataGrid that deals the specific TCm (ie, TelephoneNumber).
While it isn’t horrible at all to subclass it and do the casting I am wondering if there is any built-in xaml features that would do the casting. Or if it would be more idiomatic to use a converter?
Cheers,
Berryl
view model
public class PcmShellVm<TCm> : ... where TCm : ContactMechanism
{
public ObservableCollection<PcmDetailVm> DetailVms { get; protected set; }
public PcmDetailVm SelectedVm {
get { return _collectionView.CurrentItem as PcmDetailVm; }
set { _collectionView.MoveCurrentTo(value); }
}
}
subclassed vm
public class TelecomNumberPcmShellVm : PcmShellVm<TelecomNumber>
{
...
public IEnumerable<TelecomPcmDetailVm> CastedDetailVms { get { return DetailVms.Cast<TelecomPcmDetailVm>(); } }
public TelecomPcmDetailVm CastedSelectedVm
{
get { return (TelecomPcmDetailVm) SelectedVm; }
set { SelectedVm = value; }
}
}
EDIT
So Andrei had the right answer to the question I did not ask very clearly above. I had incorrectly assumed I needed to cast my items somewhere in xaml, as I would need to do in code.
The binding engine must be using reflection to discover properties by name however, making the cast unnecessary.
I am not sure I understood the question correctly but I think specific DataTemplates for each type would be an idea.
View :
ViewModels :
Setupcode (code behind, yeah…)
You can serve the view different VMs and it will render different things based on this. In my example there is a DataGrid for both VM types but it could have been more different.