【问题标题】:UserControl for a Row in a Grid网格中一行的用户控件
【发布时间】:2012-03-08 17:04:41
【问题描述】:

我有以下网格:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <!-- GridRow-Definition -->

    <Label Grid.Row="0" Grid.Column="0">FirstRow:</Label>
    <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Binding_To_First_Row}" />

    <Label Grid.Row="1" Grid.Column="0">SecondRow:</Label>
    <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Binding_To_Second_Row}" />

    <!-- many more Label-TextBox-Rows -->
</Grid>

问题: 有没有办法创建一个包含标签和文本框的用户控件,并以正确的方式正确对齐第一列?

【问题讨论】:

    标签: xaml user-controls grid


    【解决方案1】:

    答案是肯定的,有可能,但也许您应该使用 DataGrid 或带有 DataTemplate 的 ItemsControl。

    不过,您的问题的简单答案是,如果您需要不同网格中的网格列来同步它们的宽度,您可以使用 SharedSizeGroup 属性,例如:

    <UserControl>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="column1" Width="auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
        </Grid>
    </UserControl>
    

    然后在父元素中使用 Grid.IsSharedSizeScope="True"

    <StackPanel Grid.IsSharedSizeScope="True">
        <local:UserControl1/>
        <local:UserControl1/>
    </StackPanel>
    

    这会同步该范围内具有相同 SharedSizeGroup 的任何列(或行)(您可以有多个嵌套范围)。

    【讨论】:

    • 这正是我所要求的。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 2010-10-01
    相关资源
    最近更新 更多