automating the Grid

56 views Asked by At

I'm wanting to derive from the Grid control so that I can automate the layout of its children in a specific way, and was wondering what would be the best approach.

What I'd like to be able to do is something like :

    <l:CustomGrid>
        <Label Content="PropA" Grid.ColumnSpan="89"/>
        <TextBox />

        <Label Content="PropB"/>
        <Slider />

        <!-- this needs to span 2 columns -->
        <Label Content="Span" l:CustomGrid.SpanRow="True"
               HorizontalAlignment="Center" />

        <Label Content="PropC"/>
        <CheckBox />
    </l:CustomGrid>

Where the custom grid would automatically lay this out as a 2 column grid, with N rows, by having every even child be column 0 and every odd child be column 1, and then adding a new row after every odd child. This is easy enough by overriding OnVisualChildrenChanged to perform the logic/setting of Grid attached properties.

However at that point it appears that attached properties are not set yet, and so the custom SpanRow property hasn't been applied, meaning I can't perform the logic I need to.

So Im wondering how I could go about it a different way?

1

There are 1 answers

0
dev hedgehog On

The different way would be not to hack or derivate from Grid.

Listen to Loaded event and VisualChildrenChanged in case you change children collection at runtime after the Grid was loaded. In both cases you will have to execute same logic which as you described will run though children and change or apply new value to attached Grid.Column property.