Do events fire/evaluate even if not being called directly?

时间:2017-06-15 09:58:11

标签: c# wpf events

Here's the scenario: Let's say I've got a TextBlock, and I slap a Loaded event onto it, and in that event, I instruct it to make its Foreground orange.

So it loads, fires the Loaded event, goes to my code and carries out the foreground instruction.

And if I had this TextBlock on 1,000 ListBox items, it would fire this 1,000 times, right?

But if I don't call "Loaded" at all (leaving the textblock as-is), does it still fire the Loaded event in the background 1,000 times in this ListBox scenario?

1 个答案:

答案 0 :(得分:1)

And if I had this TextBlock on 1,000 ListBox items, it would fire this 1,000 times, right?

Yes, it would fire as each TextBlock gets loaded. Note however that not all 1,000 TextBlocks are loaded upfront if the ListBox uses UI virtualization (which it does by default).

But if I don't call "Loaded" at all (leaving the textblock as-is), does it still fire the Loaded event in the background 1,000 times in this ListBox scenario?

The Loaded event itself may still be fired for each TextBlock that gets loaded into the visual tree provided that there are any other subscribers, but since you are not handling the event you won't really notice.