我有一种情况,我需要在两个组框中的网格之间共享列的宽度,XAML 看起来像这样:
<GroupBox Header="Box A">
<StackPanel Orientation="Horizontal">
<!-- Labels -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="Labels"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="Rows"></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0">Label A</Label>
</Grid>
<!-- Fields -->
<ItemsControl Grid.IsSharedSizeScope="True" ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="Rows"></RowDefinition>
</Grid.RowDefinitions>
<TextBox Text="{Binding PropertyA}"></TextBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</GroupBox>
<GroupBox Header="Box B">
<StackPanel Orientation="Horizontal">
<!-- Labels -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="Labels"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="Rows"></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0">Label B</Label>
</Grid>
<!-- Fields -->
<ItemsControl Grid.IsSharedSizeScope="True" ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="Rows"></RowDefinition>
</Grid.RowDefinitions>
<TextBox Text="{Binding PropertyB}"></TextBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</GroupBox>
我尝试在包含子网格的 StackPanel 和 GroupBox 上将 Grid.IsSharedSizeScope 设置为 true,但这不起作用。我想知道在这种情况下我应该采取什么方法来共享网格列定义与“标签”的 SharedSizeGroup 之间的大小?
谢谢,
亚历克斯。