2011-02-14 228 views
4

我想要使用Grid.IsSharedSizeScope排列數據綁定控件顯示在網格的第一列中的一些控件旁邊的ItemsControl。WPF佈局問題與Grid.IsSharedSizeScope和ItemsControl.ItemTemplate

問題是我無法防止控件不斷垂直增長。

如何在不設置MaxHeight屬性的情況下阻止他們這樣做。我在各個地方嘗試了VerticalAlignment和VerticalContentAlignment的不同設置,但無法弄清楚。

<Grid Grid.IsSharedSizeScope="True" > 
    <Grid.RowDefinitions> 
     <RowDefinition SharedSizeGroup="RowOne" /> 
     <RowDefinition SharedSizeGroup="RowTwo" /> 
     <RowDefinition SharedSizeGroup="RowThree" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto" /> 
     <ColumnDefinition Width="Auto" /> 
    </Grid.ColumnDefinitions> 
    <SomeControl Grid.Row="0" Grid.Column="0" /> 
    <SomeControl Grid.Row="1" Grid.Column="0" /> 
    <ItemsControl Grid.Row="0" Grid.Column="1" Grid.RowSpan="3" ItemsSource="{Binding Path=SomeSource}" ItemsPanel="{StaticResource MyHorizontalStackPanel}" > 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition SharedSizeGroup="RowOne" /> 
          <RowDefinition SharedSizeGroup="RowTwo" /> 
          <RowDefinition SharedSizeGroup="RowThree" /> 
         </Grid.RowDefinitions> 
         <SomeControl Grid.Row="0" /> 
         <SomeControl Grid.Row="1" /> 
         <SomeControl Grid.Row="2" /> 
        </Grid> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </Grid> 

回答

15

嘗試使用Grid.IsSharedSizeScope壞嵌套網格,將網格和ItemsControl的並排另一網格內有兩列,好。

這是我自己的解決方案,以我自己的愚蠢:

<!-- outer grid (could be a stack panel) --> 
<Grid Grid.IsSharedSizeScope="True"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto" /> 
     <ColumnDefinition Width="Auto" /> 
    </Grid.ColumnDefinitions> 
    <!-- header --> 
    <Grid Grid.Column="0" Margin="0,10,10,0"> 
     <Grid.RowDefinitions> 
      <RowDefinition SharedSizeGroup="RowOne" /> 
      <RowDefinition SharedSizeGroup="RowTwo" /> 
      <RowDefinition SharedSizeGroup="RowThree" /> 
     </Grid.RowDefinitions> 
     <SomeControl Grid.Row="0" Grid.Column="0" /> 
     <SomeControl Grid.Row="1" Grid.Column="0" /> 
    </Grid> 
    <!-- rows --> 
    <ItemsControl Grid.Column="1" ItemsSource="{Binding Path=SomeSource}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition SharedSizeGroup="RowOne" Height="Auto" /> 
         <RowDefinition SharedSizeGroup="RowTwo" Height="Auto" /> 
         <RowDefinition SharedSizeGroup="RowThree" Height="Auto" /> 
        </Grid.RowDefinitions> 
        <!-- define your row here --> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</Grid> 
+3

雖然它已有4年了......我必須說..不要對自己;-) – 2015-03-28 14:27:00