I’m using a RadGridView to display a bunch of items in a grid. For each item, I want to switch between two different templates based on the data being given. One is a dependency property which essentially pops a text block in, the other is another RadGridView to display a table.
When put in statically, they both work individually, but I want to dynamically select these two different templates. My selector does not get called, however, and thus no template is used.
Resources:
<Window.Resources>
<DataTemplate x:Key="theBasicView">
<controls:InfoDetailsControl InfoDetail="{Binding InfoDetails}" />
</DataTemplate>
<DataTemplate x:Key="theTableView">
<telerik:RadGridView ItemsSource="{Binding DetailsTable}" />
</DataTemplate>
<analysis:DetailsTemplateSelector
BasicView="{StaticResource theBasicView}"
TableView="{StaticResource theTableView}"
x:Key="detailsTemplateSelector"
/>
</Window.Resources>
And the template selector in question:
<telerik:RadGridView.RowDetailsTemplate>
<DataTemplate>
<ItemsControl
ItemTemplateSelector="{StaticResource detailsTemplateSelector}"
/>
</DataTemplate>
</telerik:RadGridView.RowDetailsTemplate>
If it is a BasicView, then the DetailsTable should be null. Otherwise, it should be a TableView. Here is my DetailsTemplateSelector:
public class DetailsTemplateSelector : DataTemplateSelector
{
public DataTemplate BasicView { get; set; }
public DataTemplate TableView { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
FrameworkElement element = container as FrameworkElement;
if (element != null && item != null && item is ViewableRuleResult)
{
ViewableRuleResult ruleResult = item as ViewableRuleResult;
Window window = Application.Current.MainWindow;
if (ruleResult.DetailsTable == null)
{
return BasicView;
}
else
{
return TableView;
}
}
return null;
}
}
Putting a breakpoint in the SelectTemplate function never gets hit. Why is my DetailsTemplateSelector never getting called? I have a feeling that the template selector in my RowDetailsTemplate isn’t right. Let me know if you need more detail or something is unclear.
Thanks!
Fixed it. Turns out RadGridView has a property
RowDetailsTemplateSelector. Using the following XAML:And completely deleting the
RowDetailsTemplatepreviously defined, it now functions properly.