I have a WPF ListView control with ItemsSource set to a collection of item ViewModel class instances. ListView has ItemTemplate with custom control hierarchy.
I need to do certain things to those controls generated via ItemTemplate when the ListView is actually shown (if not already visible) and all of it's items are layed out (i.e. when it's safe to use ItemContainerGenerator.ContainerFromItem
).
If I subscribe to ItemContainerGenerator.StatusChanged
, when the status changes to GeneratorStatus.ContainersGenerated
there are still no items in the generator (?!? btw.). How can I be notifed when the ListView is ready for iterating over the actual controls that are bound to items, even when ItemsSource is changed at runtime on an existing control?
Now, I know I can subcribe to events of the actual control in the template, but I need to know when all of the item controls are created, doing stuf in the item's viewmodel doesn't help me since I need to do UI-related stuff, like animations, etc.
In short, there isn't such an event. You may be able to use the LayoutUpdated event, but that is fired more than once as the ListView is updated.
Keep in mind also, that if virtualization is enabled, then not all the containers will be created at once.
Your best bet would be to use an attached behavior (see here and here) for the ListViewItem, which you can apply using an implicit Style.
For example, if you created a class called MyBehavior with a boolean attached property named MyProperty with a default value of false, then the Style would be something like:
<ListView ...>
<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="local:MyBehavior.MyProperty" Value="True" />
</Style>
</ListView.Resources>
</ListView>
When your property is set to true, you know the ListViewItem has been created and should be ready to render. If not, you can attach to the Loaded event to know when to do your work.