从两个itemtemplates绑定网格的列宽

时间:2016-10-31 12:20:46

标签: wpf xaml

在一个XAML窗口中,我有两个单独的ItemsControls用于不同的ViewModelsItemsControls都有网格,其中第一列的宽度应相同。现在我有以下内容,但网格只是独立管理它们的大小。我希望第一列的宽度相同。这是我的XAML

<TabControl>
  <TabItem Header="x">
    <Grid Grid.IsSharedSizeScope="True">
      <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
      </Grid.RowDefinitions>
      <Grid Margin="10" Grid.Row="0" Grid.IsSharedSizeScope="True">
        <ItemsControl Name="inputs1" Grid.IsSharedSizeScope="True">
          <ItemsControl.ItemTemplate>
            <DataTemplate>
              <Grid >
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="auto" SharedSizeGroup="1" />
                  <ColumnDefinition Width="*" />
                  <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock VerticalAlignment="Center" Text="{Binding Description}" />
              </Grid>
            </DataTemplate>
          </ItemsControl.ItemTemplate>
        </ItemsControl>
      </Grid>

      <Grid Margin="10" Grid.Row="1" Grid.IsSharedSizeScope="True">
        <ItemsControl Name="inputs2" Grid.IsSharedSizeScope="True">
          <ItemsControl.ItemTemplate>
            <DataTemplate>
              <Grid Margin="0,0,0,5">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition SharedSizeGroup="1" />
                  <ColumnDefinition Width="*" />
                  <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock VerticalAlignment="Center" Text="{Binding Description}" />
              </Grid>
            </DataTemplate>
          </ItemsControl.ItemTemplate>
        </ItemsControl>
      </Grid>
    </Grid>
  </TabItem>
</TabControl>

他们有相同的共享组,所以他们应该是对的吗? 现在结果:https://gyazo.com/2284485127427673269dfd8e26e42682

1 个答案:

答案 0 :(得分:2)

删除较低层级中的所有Grid.IsSharedSizeScope="True"属性,并仅将其保留在跨越所有相关网格的控件中。似乎对于每个属性,都会打开一个新的共享范围,因此忽略更高级别的范围(尽管我无法从文档中读取它)。