Как выровнять отдельные сетки, созданные через шаблоны вдоль их столбцов / рядов?

StackOverflow https://stackoverflow.com/questions/4604402

  •  25-09-2019
  •  | 
  •  

Вопрос

Я думаю, что в этом случае картина стоит тысячи слов:

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>

PS - Я не хочу устанавливать конкретный с первой колонкой, но чтобы дать ему максимум с этим нужным.

Обновлять:Я пробовал ссылку Колина и сделали это:

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

Но это не работало для меня.

Это было полезно?

Решение

Вам нужно использовать SharedSizeGroup для каждого столбца.

Проверьте это руководство ...

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

Кроме того, убедитесь, что собственность по объему общего размера верно для вашей сетки:

<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>

Другие советы

Вместо использования сетки Как насчет использования 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>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top