I have 2 templates for DataGrid’s CellTemplate. When I change the items, it won’t help me select the template for me, my DisplayModeTemplateSelector won’t even be called!
What I’m wondering is if there is a way to trigger this CellTemplateSelector again when items changed? How to refresh CellTemplate in DataGrid or ListView When Content Changes
<DataGridTemplateColumn x:Name="colorRange"
Width="*"
Header="Color Range">
<DataGridTemplateColumn.CellTemplateSelector>
<local:DisplayModeTemplateSelector HeatMapTemplate="{StaticResource heatMapTemplate}" ThreshHoldTemplate="{StaticResource threshHoldTemplate}" />
</DataGridTemplateColumn.CellTemplateSelector>
</DataGridTemplateColumn>
I found this blog
http://dotdotnet.blogspot.com/2008/11/refresh-celltemplate-in-listview-when.html
I think this is similar with my problem, but I really can’t understand him! Can anyone explain it?
The solution in the blog post will not work with the
DataGridcontrol because theDataGridTemplateColumnclass doesn’t belong to the Visual Tree, and even when I tried to bind it to a static class, I didn’t suceed because of strange exceptions after property changes.Anyway there is two possible ways to solve this problem.
1) The easier way.
Using the
ObservableCollectionclass.2) The more complex way.
You can add to your item class the property which returns the object itself.
After that you can use the
ContentControl.ContentTemplateSelectorinstead of theCellTemplateSelectorlike this:And when you change the property, call the
Updatemethod somehow:The reason why I’ve set a null value to the
SelfPropertyin theUpdatemethod first, is that theSelectorwill not update a template until theContentproperty is completely changed. If I set the same object once again – nothing will happen, but if I set a null value to it first – changes will be handled.