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

后端 未结 2 1268
清酒与你
清酒与你 2021-01-12 03:00

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

\"alt

XAML:

 <         


        
相关标签:
2条回答
  • 2021-01-12 03:46

    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>
    
    0 讨论(0)
  • 2021-01-12 03:55

    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>
    
    0 讨论(0)
提交回复
热议问题