Auto refresh ItemTemplateSelector
-
18-06-2021 - |
Frage
I have ItemsControl
with multi DataTemplate
and use ItemTemplateSelector
to chose appropriate DataTemplate
of each item of ItemsSource
. I want to binding ItemTemplateSelector
to TemplateType dependency property of item. and each time TemplateType property changed, DataTemplate
change automaticaly. How can I do.
Lösung
I Search about own question. and find below code to refresh ItemTemplateSelector
:
DataTemplateSelector DataTemplateSelector = plan.ItemTemplateSelector;
plan.ItemTemplateSelector = null;
plan.ItemTemplateSelector = DataTemplateSelector;
Andere Tipps
I'd go for a different solution in your case, if you want to update the datatemplate. I would create a binding to the ItemTemplate of your ItemsControl. That binding gets a converter assigned, which contains the logic of your current ItemTemplateSelector. And the source of the binding is your TemplateType property.
The ItemTemplateSelector is just for one-shot evaluation.