0

I have a control that uses a ParentObject as its DataContext. The ParentObject has a property called ChildObject that may change. When it does, ParentObject raises the PropertyChanged event.

The control has XAML to define a ListView for the items in the List property of the ChildObject. When the ChildObject property changes in the ParentObject, the entire view is recreated, meaning that new controls are instantiated.

The ListView is actually much more complicated than the example below, so recreating it is processor intensive and takes a long time.

What are my other options? Can I cache the entire ListView for each ChildObject? How would I go about doing that?

<ListView ItemsSource="{Binding ParentObject.ChildObject.List}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Error">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <local:ErrorControl DataContext="{Binding ErrorCollection}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>
1
  • Is it the rendering that take time or creating the list take time? Commented Aug 30, 2012 at 17:19

1 Answer 1

1

The listview is creating new controls becuase of your datatemplate. With data templates, you have a one-to-one relationship between the instance of your data and the controls inside of the datatemplate. You have a few options:

1) Have you looked into virtualization? This will only render what is visible. If you have a high number of objects, you definitely want to consider this.

2) Have you thought about re-working your view-model to minimize the change in your collections? Instead of adding/removing objects anytime an error changes. Re-use the objects in the collection and only add/remove when your total count changes.

I've done both of these to slow changes to the view from the view-model. They both improved performance significantly, but I had hundreds of objects on the screen.

Sign up to request clarification or add additional context in comments.

5 Comments

My issue isn't that the collection items are changing, it is that the collection itself is changing. There are only a (relatively) few items in the collection, but the ListView used to display them is quite large (larger than the example I gave). That is why I'd like to investigate caching the entire ListView object and replacing it whenever ChildObject changes.
@ken Virtualization is what you need. Implement option 1.
Thanks, but I don't think I'm explaining myself very well. I actually want the opposite of virtualization: I want the entire ListView to be rendered and cached, and then swapped with other cached ListViews whenever the ChildObject property changes on the ParentObject.
Perhaps you should use multiple collections then. You could datatemplate each collection with its own listview. Honestly, I think caching large amounts of info is less performant than virtualization. Rendering is expensive. What is the percentange of items you are displaying on the screen. If it's a high percentange, maybe virtualization doesn't get you much. If it is a low percentage, virtualization is a good choice hands-down.
Thanks for your time and effort. I ended up doing the "caching" in code-behind. I'll post my method as an answer later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.