Is there a way to use WPF Grid without specifying absolute cell coordinates for every item?
Asked Answered
T

3

10

My typical form with Grid look like this in XAML:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>

        ....

        <TextBlock Grid.Row="6" Text="Component"/>
        <TextBox Grid.Row="6" Grid.Column="1" Text="{Binding Component.Name}"></TextBox>
        <TextBlock Grid.Row="7" Text="Bussiness Process"/>
        <TextBox Grid.Row="7" Grid.Column="1" Text="{Binding BusinessProcess.Name}"></TextBox>

    </Grid>

Is there a way to avoid specifying th exact Row/Column coordinates?

Basically, I would like to be able to reorder my controls in the Grid quickly by copying XAML around and now I have to change the coordinates which is awkward. I don't want to reorder them in the Designer because it adds some unnecessary properties to the items.

Turne answered 25/9, 2009 at 16:5 Comment(0)
N
2

No, there is no way to avoid Row/Column numbers. If you don't want to install MS Blend, you can try wonderful and free Visual Studio addon from Karl Shifflett: XAML Power Toys. Among other useful things it allows you to play with Grid cells...

Niemeyer answered 25/9, 2009 at 16:19 Comment(2)
This, unfortunately, doesn't help with the "extra properties" issue, though, since it's still using the VS Designer. Great toolset, though.Bogard
Looks cool, it can just generate a simple form from the scratch, I'll give it a try! Spasibo )Turne
B
3

No. Unfortunately, the Grid works by having the Grid.Row and Grid.Column attached properties defined on the children - they must be specified on each child.

BTW - Blend does a better job of letting you move these things around without the "extra" properties appearing. You may want to try giving that a shot for these types of situations.

Bogard answered 25/9, 2009 at 16:12 Comment(2)
IS there any other layout control that can organize my controls into "columns"?Turne
You can use a StackPanel with each "row" containing a horizontally oriented stackpanel - in that case, elements will align. To get a perfect "grid", though, the elements would have to naturally be the same size.Bogard
N
2

No, there is no way to avoid Row/Column numbers. If you don't want to install MS Blend, you can try wonderful and free Visual Studio addon from Karl Shifflett: XAML Power Toys. Among other useful things it allows you to play with Grid cells...

Niemeyer answered 25/9, 2009 at 16:19 Comment(2)
This, unfortunately, doesn't help with the "extra properties" issue, though, since it's still using the VS Designer. Great toolset, though.Bogard
Looks cool, it can just generate a simple form from the scratch, I'll give it a try! Spasibo )Turne
H
0

I may be very late to the party, but anyway... ;-)

Thomas Levesque's SimpleGrid could be a suitable solution:

<my:SimpleGrid Rows="Auto,5,*" Columns="60,*">
    ...
</my:SimpleGrid>

See his blog for more details: https://thomaslevesque.com/2010/07/20/wpf-a-simpler-grid-using-xaml-attribute-syntax/

Honeycutt answered 1/8, 2023 at 10:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.