1

I'm not being able to update a single item's DataTemplate on a list during runtime. In detail, here is what I'm trying to accomplish.

I have a Listbox where the items can have different states (Collapsed, expanded, disabled, etc), each with a different layout. I'm using a TemplateSelector to choose the correct DataTemplate according to a property on my class and that's working all great when I first create the list, the items are shown properly. However, when I change the property that sets the DataTemplate in runtime, the NotifyPropertyChanged is called and the information of the item is updated on the list, but not the DataTemplate. For example: I have a collapsed item with a label X that i want to expand. I click on the item and the label changes to Y but the DataTemplate doesn't update.

Any idea on how I can do this? Can't the DataTemplate be updated during runtime unless it is for the whole list?

I'll appreciate any help.

Ana Figueira
  • 43
  • 1
  • 7
  • Good guess, but as you found not the correct one. I believe the solution to your problem is in the answer to this question http://stackoverflow.com/questions/8715315/how-to-trigger-datatemplateselector-when-property-changes using a DataTrigger to change the template. DataTriggers are ... triggered ... on INPC property changes. See if that's correct, and if so, we can mark this as a dupe (that's not a bad thing!). –  Jul 07 '15 at 14:43
  • Thank you for your help @Will but the solution in that question doesn't work. I get the error: "The attachable property 'Triggers' was not found in type 'Style'.". Is there anyway I can force the listitem to be re-rendered so it updates the datatemplate? – Ana Figueira Jul 08 '15 at 10:07
  • There's got to be an analogous version for WP flavored WPF. I don't know what that is, unfortunately. But there lies your solution. –  Jul 08 '15 at 13:15
  • Well, it's not a direct answer to your question. However, a good solution to your problem. Make `UserControl` and use it inside your data template. Now, to change the state you can call methods on this `UserControl` and it will update. You can use animations via storyboard too. – parveen Jul 15 '15 at 05:38
  • @CodetrixStudio, thank you! That solved it! I'm new at this so should you submit it as an answer so I can accept it? – Ana Figueira Jul 27 '15 at 11:18
  • You are welcome, submitted as an answer. – parveen Jul 27 '15 at 11:59

1 Answers1

0

Make UserControl and use it inside your data template. Now, to change the state you can call methods on this UserControl and it will update. You can use animations via storyboard too.

parveen
  • 1,939
  • 1
  • 17
  • 33