【问题标题】:WPF Grid.IsSharedSizeScope between two parent controls?两个父控件之间的WPF Grid.IsSharedSizeScope?
【发布时间】:2015-02-10 13:51:54
【问题描述】:

我有一种情况,我需要在两个组框中的网格之间共享列的宽度,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,但这不起作用。我想知道在这种情况下我应该采取什么方法来共享 Grid 列定义与“Labels”的 SharedSizeGroup 之间的大小?

谢谢,

亚历克斯。

【问题讨论】:

    标签: .net wpf xaml .net-4.5 wpf-4.5


    【解决方案1】:

    Grid 包围你的两个GroupBoxes 并设置Grid.IsSharedSizeScope

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:sys="clr-namespace:System;assembly=mscorlib"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Grid Grid.IsSharedSizeScope="True">
        <Grid.Resources>
          <x:Array x:Key="xItems" Type="sys:String">
            <sys:String>Hello</sys:String>
            <sys:String>World ddd</sys:String>
          </x:Array>
          <x:Array x:Key="xItems2" Type="sys:String">
            <sys:String>Hello long fsdfhuzweb kbhui</sys:String>
            <sys:String>World</sys:String>
          </x:Array>
        </Grid.Resources>
    
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*"/>
          <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
    
        <GroupBox Grid.Column="0" Header="Box A">
          <StackPanel Orientation="Horizontal">
            <!-- Labels -->
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="Labels" />
              </Grid.ColumnDefinitions>
              <Label Grid.Column="0" Grid.Row="0">Label A</Label>
            </Grid>
            <!-- Fields -->
            <ItemsControl ItemsSource="{Binding Source={StaticResource xItems}}">
              <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                  <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
              </ItemsControl.ItemsPanel>
              <ItemsControl.ItemTemplate>
                <DataTemplate>
                  <Grid>
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition SharedSizeGroup="Fields" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                      <RowDefinition SharedSizeGroup="Rows" />
                    </Grid.RowDefinitions>
                    <TextBox Text="{Binding Mode=OneWay}" />
                  </Grid>
                </DataTemplate>
              </ItemsControl.ItemTemplate>
            </ItemsControl>
          </StackPanel>
        </GroupBox>
    
        <GroupBox Grid.Column="1" Header="Box B">
          <StackPanel Orientation="Horizontal">
            <!-- Labels -->
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="Labels" />
              </Grid.ColumnDefinitions>
              <Grid.RowDefinitions>
                <RowDefinition SharedSizeGroup="Rows" />
              </Grid.RowDefinitions>
              <Label Grid.Column="0" Grid.Row="0">Label B long</Label>
            </Grid>
            <!-- Fields -->
            <ItemsControl ItemsSource="{Binding Source={StaticResource xItems2}}">
              <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                  <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
              </ItemsControl.ItemsPanel>
              <ItemsControl.ItemTemplate>
                <DataTemplate>
                  <Grid>
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition SharedSizeGroup="Fields" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                      <RowDefinition SharedSizeGroup="Rows" />
                    </Grid.RowDefinitions>
                    <TextBox Text="{Binding Mode=OneWay}" />
                  </Grid>
                </DataTemplate>
              </ItemsControl.ItemTemplate>
            </ItemsControl>
          </StackPanel>
        </GroupBox>
      </Grid>
    </Page>
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 2023-03-06
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多