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>
More properly will be move your VMs to the Assembly which can be referenced from your project with DataTemplates.
In case if you haven't ability to move VM's but can modify it.. the solution is implement some interface from shared assembly.
In case if you don't have ability to modify VM the most robust solution - use .NET reflection for get value from the property of passed instance.