Background
I am creating a custom wpf panel. To help lay out the child items it uses a secondary list (similar to Grid.RowDefinitions or Grid.ColumnDefinitions) that I call "layouts". Each layout has a couple dependency properties, and child items use an attached property to determine where they're placed as seen below.
<Panel>
<Panel.Layouts>
<Layout/>
<Layout Attachment="Right"/>
<Layout Attachment="Left" Target="0"/>
</Panel.Layouts>
<ChildItem Panel.Layout="0"/>
<ChildItem Panel.Layout="1"/>
<ChildItem Panel.Layout="2"/>
<Panel/>
Obviously, things are simplified a bit, but long story short: I need to process the layouts as they are added before the "Arrange" process can occur. I have created a custom collection and I can see the items as they are added (see code below), but the items just have their default properties.
LayoutCollection: IList
{
public int IList.Add(object value)
{
// When first starting, this line always returns the default value, not the one set in XAML
Attachment a = (value as Layout).Attachment;
// Other code happens below...
}
}
However, when I look at the collection after the panel has been initialized the properties are all set correctly. Which brings me to my question:
Question
At what point during the process between XAML and initialization of the panel do the items get assigned their properties, and how are they assigned? I need to somehow hook on to that and run a bit of code.
My idea is that the Panel layout system of WPF is not really designed to be intercepted the way you wanted it to be.
But there is a mechanism in-place to 'sync' your custom DependencyProperties. And the question is WHEN do you need these properties during the layout passes?
If it is during ArrangeOverride, you have to set AffectsArrange
. Or AffectsMeasure
when during MeasureOverride. There are some other flavors of this but I think either of these two can satisfy your requirement.
For example, if you choose AffectsArrange and your Panel is notified of List.Add, ArrangeOverride will be invoked and the newly added item can be used within your arrangement logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With