Exception on Toggle in/out of view in Win8.1


In Win8.1, if you have a ItemsControl derived type that removes and re-adds some portion of its children and you're using the c:View.Model binding on a data template to convert child view models into their views, you hit an issue.

The issue is this. The OnModelChanged event gets fired every time that the child appears or disappears, which in and of itself would not be bad. However, the following pattern is then employed ...
  1. Use the ViewLocator to get the view.
  2. Set the ContentProperty on the target to the view.
The problem lies in that the ViewLocator attempts to use a cached view by default and that view is still part of the visual tree. Therefore, the setting the content property fails because the view is already the child of another element in the tree.

This has been tested with several Syncfusion controls that employ this visual hiding/reshowing.

The workaround is to add this override, which trades out the exception (and failure to display the view-model with instead a memory increase as the view keeps being created.
    public override object GetView(object context = null) {
        return null;
Closed Feb 20, 2014 at 6:01 PM by EisenbergEffect
Moved to github


tibel wrote Dec 31, 2013 at 9:54 AM

a better workaround is to set CacheViews property to false in the constructor.

may attach a sample that show the issue, please.