Sabuncu
Sabuncu

Reputation: 5274

What is a better way to organize WPF elements?

I have the following XAML:

<Window x:Class="WpfTest.Window2" ...
    <Grid>
        <Grid.RowDefinitions>
            <!-- 0: Row Zero -->
            <RowDefinition Height="30" />
            <!-- 1: Row 1 -->
            <RowDefinition Height="30" />
            <!-- 2: Row 2 -->
            <RowDefinition Height="30" />
            ...
            <!-- and so on. -->

       <!-- 0: Row Zero elements -->
       <Label Grid.Row="0" ... />
       <TextBlock Grid.Row="0" ... />

       <!-- 1: Row 1 elements -->
       <Label Grid.Row="1" ... />
       <TextBlock Grid.Row="1" ... />
...

At design time, if I have to insert a row, say between row 0 and row 1, I then have to renumber the Grid.Row attached properties for all the Label and TextBlock elements because now they are all shifted by plus one.

Is there an easier way of doing this so that I can just insert a row and not have to adjust the remaining items below that row?

Upvotes: 1

Views: 710

Answers (1)

Drew Noakes
Drew Noakes

Reputation: 311255

As stated in the comments, there's no way to do this directly with Grid. You'll have to reflow the indices manually unfortunately.

Another option, if this is really important to you, is to create a custom panel and use some non-visual child element as row separators. Internally that panel could still use a Grid to position its children, and it would have to set the Grid.Row and/or Grid.Column attached property values itself.

Upvotes: 1

Related Questions