简体   繁体   中英

How to split a stackpanel like a grid

I need to show details of an object in the ui. I have the following code which defines a data template for a class Category . I am trying to get a layout where pairs of member name and value will be stacked vertically. However I want member names to be of same width and same for values. What should I do to get this layout?

    <DataTemplate DataType="{x:Type local:Category}">
        <Grid HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="4*" SharedSizeGroup="a" />
                <ColumnDefinition Width="6*" SharedSizeGroup="b" />
            </Grid.ColumnDefinitions>
            <StackPanel>
                <StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal" Grid.ColumnSpan="2">
                    <TextBlock Text="Id" Grid.Column="0" Grid.IsSharedSizeScope="True" />
                    <TextBlock Text="{Binding Path=Id}" Grid.Column="1"/>
                </StackPanel>
                <StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal" Grid.ColumnSpan="2">
                    <TextBlock Text="Name" Grid.Column="0" Grid.IsSharedSizeScope="True" />
                    <TextBlock Text="{Binding Path=Name}" Grid.Column="1"/>
                </StackPanel>
            </StackPanel>
        </Grid>
    </DataTemplate>

我想你可以把Grid分成两行并且不使用StackPanels?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM