Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do declared XAML list items have their dependency properties set?

Tags:

c#

list

wpf

xaml

panel

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.

like image 900
bidanshi Avatar asked Aug 11 '15 06:08

bidanshi


1 Answers

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.

like image 197
tgpdyk Avatar answered Nov 14 '22 23:11

tgpdyk