问题
Is there a way to get a event from a Panel
when a child is added or removed?
I'm deriving from a WrapPanel
atm.
回答1:
There is no public event or protected override that tracks changes Children
membership. However a change in the membership of the Children
property will ultimately result in a LayoutUpdated
event.
If you just need to know if the members have been changed then a simple copy of the last count of children would suffice. However if you need to keep track of which members have been added or removed then you will have your work cut out for you, maintaining shadow collection, comparing the collections and ensure you don't hold on to entries that should be removed from your shadow collection.
Bear in mind that LayoutUpdated
can happen fairly frequently for all sorts of other reasons so any code you attach to it needs be done as quick as possible. Since any changes you might make to the visual tree might also trigger another LayoutUpdated
care is needed to avoid creating an infinite loop.
来源:https://stackoverflow.com/questions/5855094/silverlight-get-event-when-children-of-panel-changes