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

一笑奈何 提交于 2019-12-19 05:50:07

问题


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

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.


回答1:


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>



回答2:


Instead of using Grid how about using DataGrid?

<Grid>

    <DataGrid ItemsSource="{Binding}" 
             AutoGenerateColumns="False" 
             IsReadOnly="True" 
             CanUserAddRows="False" 
             ColumnHeaderHeight="0" 
             GridLinesVisibility="None">

        <DataGrid.Resources>

            <Style TargetType="DataGridCell" x:Key="NameStyle">
                <Setter  Property="Background" Value="LightBlue"/>
                <Setter Property="BorderBrush" Value="LightBlue"/>
            </Style>

            <Style TargetType="DataGridCell" x:Key="AgeStyle">
                <Setter  Property="Background" Value="LightPink"/>
                <Setter Property="BorderBrush" Value="LightPink"/>
            </Style>

        </DataGrid.Resources>

        <DataGrid.Columns>

            <DataGridTextColumn Binding="{Binding Name}" 
                                CellStyle="{StaticResource ResourceKey=NameStyle}"/>

            <DataGridTextColumn Binding="{Binding Age}" 
                                CellStyle="{StaticResource ResourceKey=AgeStyle}"/>

        </DataGrid.Columns>

    </DataGrid>

</Grid>


来源:https://stackoverflow.com/questions/4604402/how-to-align-separate-grids-created-via-templates-along-their-columns-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!