Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align separate Grids created via templates along their columns / rows?

I thinks that in this case A picture is worth a thousand words:

alt text

XAML:

 <Grid>
    <ItemsControl ItemsSource="{Binding Persons}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Name}" Background="LightBlue"/>
                    <TextBlock Text="{Binding Age}" Background="LightPink" Grid.Column="1"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

p.s - I don't want to set a specific with to the first column, but to give it the max with that it needs.

Update: I've tried ColinE's link and done this:

 <Grid.ColumnDefinitions>
                        <ColumnDefinition SharedSizeGroup="A" Width="Auto"/>
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

but it didn't worked for me.

like image 622
Erez Avatar asked Jan 05 '11 13:01

Erez


1 Answers

You need to use a SharedSizeGroup for each column.

Check out this tutorial ...

http://blogs.interknowlogy.com/johnbowen/archive/2007/08/27/21132.aspx

Also, ensure that the shared size scope property is true for your grid:

<Grid  Grid.IsSharedSizeScope="True">
    <ItemsControl ItemsSource="{Binding Persons}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Name}" Background="LightBlue"/>
                    <TextBlock Text="{Binding Age}" Background="LightPink" Grid.Column="1"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>
like image 74
ColinE Avatar answered Nov 04 '22 02:11

ColinE