How does the FrameworkElement.Loaded event work with virtualization?

Each FrameworkElement has a Loaded event that fires when ...

the tree is not only built and initialized, but the layout works on it, the data is attached, it is connected to the rendering surface (window), and you are on the verge of being rendered. When we reach this point, we can draw a tree by passing the Loaded event, starting at the root. This event corresponds to the IsLoaded property.

What is the expected behavior for a segment / tree element when virtualizing off-screen appears on the screen, run the screen again and again?

I would expect the event to not fire twice, so not when it appears on the screen again, but the time of the first shot is not clear to me.

Luke

+4
source share
2 answers

Convert comment to response: as far as I know, the FrameworkElement.Loaded event will fire every time you scroll through the list. For example - ListBox . I tried applying animation to load a new item in the ListBox , but the event was fired each time I scroll. Everything was so because:

But the default value of ScrollViewer.CanContentScroll set to true , which means that when you scroll through each item, a new one is created. This is done to improve the loading of large lists with a large number of items. If it is set to false , then after all the elements are loaded into memory, which may affect performance if there are many elements. And the FrameworkElement.Loaded event will fire only once (at least this should be a test for a small ListBox ).

Quote from a good source about CanContentScroll :

ScrollViewer currently allows you to use two scrolling modes: smooth scrolling by pixels (CanContentScroll = false) or separate scrolling by points (CanContentScroll = true). Currently, WPF only supports user interface virtualization when scrolling through an item. Pixel scrolling is also called “physical scrolling,” and item-based scrolling is also called “logical scrolling.”

Even in the case of the Visibility property, that is:

 Visibility.Collapsed -> Visibility.Visible 

The FrameworkElement.Loaded event is raised only after the first conversion visibility. If you later change the visibility property, the event will not fire.

I think this event It should be used for static controls that are displayed only once, for example, when the program starts.

+1
source

Well, this is rather strange, maybe the threading problem stops my debugger output (?), But the attached property, which I use to apply random jilt to the elements, fires again and again when scrolling, so the loaded event is fired every time.

Anatoly replied in the comments, so I will ask him to publish the answer and get his prize.

+2
source

Source: https://habr.com/ru/post/1491658/


All Articles