DataTemplate Change doesn't work

7 posts, 0 answers
  1. Doug
    Doug avatar
    49 posts
    Member since:
    May 2012

    Posted 11 Jul 2013 Link to this post

    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.
  2. Deyan
    Admin
    Deyan avatar
    2037 posts

    Posted 15 Jul 2013 Link to this post

    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.
  3. DevCraft banner
  4. Doug
    Doug avatar
    49 posts
    Member since:
    May 2012

    Posted 15 Jul 2013 Link to this post

    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
  5. Deyan
    Admin
    Deyan avatar
    2037 posts

    Posted 16 Jul 2013 Link to this post

    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.
  6. Doug
    Doug avatar
    49 posts
    Member since:
    May 2012

    Posted 16 Jul 2013 Link to this post

    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.
  7. Doug
    Doug avatar
    49 posts
    Member since:
    May 2012

    Posted 16 Jul 2013 Link to this post

    I managed to do it using converters, it was really simple. Thanks for the advice.

    Best Regards Doug
  8. Deyan
    Admin
    Deyan avatar
    2037 posts

    Posted 17 Jul 2013 Link to this post

    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.
Back to Top
DevCraft banner