1

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.

2 Answers 2

4

I Search about own question. and find below code to refresh ItemTemplateSelector :

DataTemplateSelector DataTemplateSelector = plan.ItemTemplateSelector;
plan.ItemTemplateSelector = null;
plan.ItemTemplateSelector = DataTemplateSelector;
2

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.

8
  • ItemTemplate is a property of ItemsControl and I cannot Bind Item to it. Jul 2, 2012 at 8:17
  • what do you mean you cannot bind to it? <ItemsControl ItemTemplate={Binding Path=TemplateType,Converter={StaticResource yourconv}} ... I know that does work, I used that pattern already Jul 2, 2012 at 8:25
  • When I Use ItemTemplate="{Binding Converter={StaticResource myconv}}", get null value in converter function. I expect that get my item in converter function. Jul 2, 2012 at 8:46
  • When I use above command, my converter function run only one time. But I want to chose template for each item in ItemsSource Items. Jul 2, 2012 at 9:07
  • ok then create a datatemplate with just ContentControl and do the Binding to the Template property of that ContentControl. Then it will be evaluated per item. Jul 2, 2012 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.