-1

I'm new to Windows Phone and writing an application that uses LongListSelector with group to display data. What I can't implement is, I'm trying to change LongListSelector's ItemTemplate from code by selecting an item of LLS. I tried lots of things but I think they're all for loading, I cannot change an item's template.

What should I use to change an item's template of LLS by selecting it?

T3sTR
  • 71
  • 8
  • Take a look at [this blog post](http://msmvps.com/blogs/siva/archive/2013/01/30/dynamic-item-templates-for-longlistselector.aspx) – Pantelis Apr 16 '14 at 11:46
  • This is about styling, but I need to change the template. Thanks – T3sTR Apr 16 '14 at 12:22
  • You can set the Template in a Style. Still, this doesn't answer how you would select a template for the Selected VisualState. I think there's no such thing as dynamic template selector in WindowsPhone. A workaround would be to have a boolean `IsSelected` property in your binding class that you have to manually update. You would then bind that property to the style's template and with a `ValueConverter` you would return the appropriate Template for the Selected/Unselected items. – Pantelis Apr 16 '14 at 12:58
  • Should I ask it to windows phone part of stackoverflow? I've just found it :) – T3sTR Apr 16 '14 at 13:49
  • Thats not a coding community. What is that you want to ask? Did you try what I suggested? – Pantelis Apr 16 '14 at 14:06
  • I've noticed that wasn't a code community. I've tried but I couldn't do what I want. There must be a way to create template in code and assign it to item.. Because I add buttons images etc to old template, it have to be done that way – T3sTR Apr 16 '14 at 14:21
  • You can navigate the visual tree and set the template of your item manually. – disklosr Apr 16 '14 at 15:30
  • Sorry, what I suggested isn't possible actually. But I've found this and if you combine it with the IsSelected property of each item's datacontext you will get the desired outcome. http://www.geekchamp.com/articles/implementing-windows-phone-7-datatemplateselector-and-customdatatemplateselector – Pantelis Apr 16 '14 at 15:56
  • @PiRhAnAs can you give an example about that? Pantelis in my searches I found all of that links but I can't handle the problem yet. I gave a break about it for now. Thanks for helping.. – T3sTR Apr 16 '14 at 20:26

1 Answers1

0

Sorry for the late reply, I must have missed a notification.

So, my second suggestion still won't work. It's only for static templates, meaning once your Items DataTemplate is loaded, there's no way to change them.

What you can do though, is have the XAML of your normal and selected templates in a style and tweek their visibility based on VisualState. I've edited the sample disklosr posted to illustrate what I mean, notice how the StoryBoard of the CustomUserControl changes the visibility of the NormalGrid and SelectedGrid. You can have any layout you wish inside those two grid's bound to your dataItems.

Sample

Pantelis
  • 2,060
  • 3
  • 25
  • 40
  • Thanks for helping! I'm trying more complicated way and I'm stuck. I'm using onContentChanged for changing template but it is fired on start and when adding new items. When I change an item on ObservableCollection it won't fired. I also tried to delete and add changed item but it won't fire onContentChanged also. Now I need to call onContentChange method by code, but how.. – T3sTR Apr 22 '14 at 14:58
  • Look at the project I've posted. Thats the only way to achieve what you want. There's no other way to dynamically change Item templates on LongListSelector. Also don't forget to mark an answer, if this solves your problem – Pantelis Apr 23 '14 at 11:04
  • At last I can do what I want, thank you for helping! I designed two templates as one and changed tapped states' visibilty to collapsed. When click occurs, I find the StackPanel as FrameworkElement and change visibilty property to visible, it works! Thanks @Pantelis – T3sTR Apr 24 '14 at 07:47