denis morozov
denis morozov

Reputation: 6316

ContentTemplateSelector does not re-evaluate on Binding changed

PREFACE

I created two different data DataTemplates to allow faster load time of my datagrid cells. One is basically a textblock, the other a more expensive UserControl. On load, the data template selector decides whether it needs to load a cheap vs. more expensive control, which increases first time load/performance from ~12 seconds down to ~2, because most of the cells don't need to be complex and the time that it takes to load a simple template is virtually nothing compared to the other.

Anyway, it works great on load. But, when I change the data underneath, which raises the property changed and should force the DataTemplateSelector to call SelectTemplate() again to re-evaluate which data template to be used - well, it doesn't.

What is interesting is that, after above failure, when I click on a cell, it does call the SelectTemplate() and flips the DataTemplate if needed.

CODE

<DataGrid>
 .....
<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding Intervals[0].Self}" 
                      ContentTemplateSelector="{StaticResource TxtVsExpensiveCell_TemplateSelector}"/>
         </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>

Note, the more complex control that is selected by the DataTemplate Selector works perfectly as I had it tested without the DataTemplateSelector. Also, a note on Binding Intervals[0].Self - self is a property that returns "this", the ref of the Object that I am binding to, the only reason for that, is to (when the Self is raised) to force the DataTemplate to be re-evaluated. Apparently that doesn't quite work.

QUESTION

How do I force the data template to re-evaluate?

Supporting code*

this is my resource dict (nothing fancy)

<DataTemplate x:Key="SimpleTemplate">
    <TextBlock DataContext="{Binding}"/>
</DataTemplate>

<DataTemplate x:Key="ComplexTemplate">
    <Views:ComplexCell DataContext="{Binding}"/>
</DataTemplate>

<Views:MyTemplateSelector
    x:Key="TxtVsExpensiveCell_TemplateSelector"
    SimpleTemplate="{StaticResource SimpleTemplate}"
    ComplexTemplate="{StaticResource ComplexTemplate}"/>

and obviously I have MyTemplateSelector class that overrides SelectTemplate and works just fine...

Upvotes: 5

Views: 2598

Answers (0)

Related Questions