Grid 内部的 StackPanel - 如何将其元素放在不同的列中

StackPanel inside Grid - how to put its elements in different columns

这是我的代码:

<Grid x:Name="SourceGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>

    </Grid.RowDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" Text="Headers" />
        <TextBlock Grid.Row="0" Grid.Column="1" Text="Tags" />



    <ItemsControl Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding Path=AllHeaders.Fields}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding}" />
                    <ComboBox ItemsSource="{Binding ElementName=SourceGrid, Path=DataContext.Tags}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

如您所见,我在 StackPanel 中的 DataTemplate 中有一个 TextBlock 和一个 ComboBox。我想要做的是将 TextBlock 放入网格的 Column=0 中,将 ComboBox 放入网格的 Column=1 中。我怎样才能做到这一点? Grid.Column 属性 在 TextBlock 和 ComboBox 中都无法访问。

这是您的新布局:

<Grid x:Name="SourceGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0"
                   Grid.Column="0"
                   Text="Headers" />
        <TextBlock Grid.Row="0"
                   Grid.Column="1"
                   Text="Tags" />

        <ItemsControl Grid.Row="1"
                      Grid.ColumnSpan="2"
                      ItemsSource="{Binding Path=AllHeaders.Fields}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="SourceGrid">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding}"
                                   Grid.Column="0" />
                        <ComboBox ItemsSource="{Binding Path=DataContext.Tags, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid},AncestorLevel=2}}"
                                  Grid.Column="1" />
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>

  <Grid x:Name="SourceGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0"
                   Grid.Column="0"
                   Text="Headers" />
        <TextBlock Grid.Row="0"
                   Grid.Column="1"
                   Text="Tags" />

        <ItemsControl Grid.Row="1"
                      Grid.ColumnSpan="2"
                      ItemsSource="{Binding Path=AllHeaders.Fields}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="SourceGrid">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding}"
                                   Grid.Column="0" />
                        <ComboBox ItemsSource="{Binding Path=DataContext.Tags, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ItemsControl}}}"
                                  Grid.Column="1" />
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>