My data template and Xaml reside in AssemblyX and my view model in AssemblyY but AssemblyX doesn't (and cannot) reference AssemblyY.
Therefore at the moment I can't cast the item object parameter to the type I need as follows for example
public class MyContentTemplateSelector : DataTemplateSelector
{
public DataTemplate ServiceTemplate { get; set; }
public DataTemplate GroupTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
MyViewModel vm = item as MyViewModel; //Cannot resolve MyViewModel
return vm.IsGroup ? GroupTemplate : ServiceTemplate;
}
}
In reality, the SelectTemplate method doesn't need the whole View Model to work out which template to select, it only needs a boolean property belonging to the ViewModel.
How can I pass the value of a property into the SelectTemplate rather than the whole model?
At the moment in my xaml I've set this up
<internal:MyContentTemplateSelector x:Key="MyContentTemplateSelector" ServiceTemplate="{StaticResource TemplateX}" GroupTemplate="{StaticResource TemplateY}"/>
<Style x:Key="ShippingServiceListStyle" TargetType="ItemsControl" >
<Setter Property="ItemTemplateSelector" Value="{StaticResource MyTemplateSelector}" />
...
...
</Style>
You can't choose or change what gets passed to the
DataTemplateSelector
as it's the framework that calls this method for you.But if you don't have access to the actual view model type, you could use the dynamic keyword to bypass the static type checking performed by the compiler: