This is a migrated thread and some comments may be shown as answers.

DataTemplate Change doesn't work

6 Answers 94 Views
DataBoundListBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Doug
Top achievements
Rank 1
Doug asked on 11 Jul 2013, 11:53 AM
Hi Telerik

So I have my items loaded in the lisbox, I tap on one of them, and on tap I am changing the template in the viewmodel. But nothing is happening on the listbox. But when I scroll down about a page or more, and scroll back up to it again the change has taken effect.
I am implementing INotifyPropertyChanged in the viewmodel as well. What could be the problem? Do I need to post code as well?

Regards Doug.

6 Answers, 1 is accepted

Sort by
0
Deyan
Telerik team
answered on 15 Jul 2013, 07:41 AM
Hello Doug,

Thanks for writing.

Could you please isolate the case in a sample project and send it to us for further investigation?

Normally, when changing the ItemTemplate of RadDataBoundListBox there is a routine triggered that refreshes all realized items using the new template. It seems however that there is something in your scenario that prevents this from happening and we are willing to take a look at it.

Thanks for your time.

Regards,
Deyan
Telerik
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
0
Doug
Top achievements
Rank 1
answered on 15 Jul 2013, 11:47 AM
Hi

Here is the project. 

https://docs.google.com/file/d/0Bx2MbFVTOgexSGotWU8tNlZmLVk/edit?usp=sharing

I hope you can figure out what is that im missing.

Best Regards
0
Deyan
Telerik team
answered on 16 Jul 2013, 08:51 AM
Hi Doug,

Thanks for writing back and for the provided project.

The reason for the behavior you are observing is the fact that you are using a template selector to provide different templates for the items. Although you are implementing the INotifyPropertyChanged interface in your ViewModel and fire the event when a property changes, you never use the Template property exposed by the ViewModel in the DataTemplate defined for the items in the listbox. Therefore the control never gets informed about this property change. Additionally, you cannot use this approach to notify the control that a template defined in a template selector has changed.

In your scenario, there is much simpler solution: using a single DataTemplate for the items within the list box and using converters to change the appearance of a specific element within the data template based on a value defined in your View Model.

Let me know if I can be of further assistance here.

Regards,
Deyan
Telerik
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
0
Doug
Top achievements
Rank 1
answered on 16 Jul 2013, 01:06 PM
Hi thank you for your fast reply.

Are you telling me that there is no way i can make this work using DataTemplateSelector? Because I would like to stick to that solution instead of using converters because it is more elegant and in case i need to extend the the control it would be easier.

I cant figure out where i could bind the Template property so that the PropertyChange takes effect.

In the meanwhile im going to play around with the converter.
0
Doug
Top achievements
Rank 1
answered on 16 Jul 2013, 02:04 PM
I managed to do it using converters, it was really simple. Thanks for the advice.

Best Regards Doug
0
Deyan
Telerik team
answered on 17 Jul 2013, 12:10 PM
Hi Doug,

Thanks for getting back to me.

The Converters solution is also a good approach, especially in your case when the different data templates are identical in their structure.

As for the implementation with a Data Template selector I want to make the following remarks:

When you implement the INotifyPropertyChanged interface in your ViewModel and fire the PropertyChanged event upon updates, this impacts the bindings within an existing Data Template assigned to the visual container which is currently associated with this ViewModel. In your particular scenario, you update a Template property and fire the PropertyChanged event but this property is not used within a DataTemplate directly but by a DataTemplateSelector which decides which template to select based on its value. Since the DataTemplateSelector never subscribes for PropertyChanged events exposed by the viewmodels (as it does not have access to them until after the ListBox is bound to a datasource), it cannot handle these changes.

I hope this puts some more clarity on how things currently work with DataTemplateSelectors.

Let me know should you have further questions.

Regards,
Deyan
Telerik
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
Tags
DataBoundListBox
Asked by
Doug
Top achievements
Rank 1
Answers by
Deyan
Telerik team
Doug
Top achievements
Rank 1
Share this question
or