WPF - Stackpanel赢了

时间:2014-10-11 07:26:52

标签: wpf wpf-controls

我有一个stackpanel,我动态添加50x50矩形。我希望它每行显示6个,并在接近每个阈值时自动添加一行。 (即12个矩形== 2行。)我似乎无法正确包装它。这是我的XAML代码(我已经切断了其他行中的内容,因为我怀疑它们不相关):

 <Grid>
 <Grid.ColumnDefinitions>
      <ColumnDefinition Width="60"/>
      <ColumnDefinition Width="140"/>
      <ColumnDefinition Width="2*"/>
      <ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
       <RowDefinition Height="80"/>
       <RowDefinition Height="80"/>
       <RowDefinition Height="230"/>
       <RowDefinition Height="auto"/>
 </Grid.RowDefinitions>
      <TextBlock FontWeight="Bold" FontSize="14" Grid.ColumnSpan="3">Swatch Thumbnails</TextBlock>
      <StackPanel x:Name="sp_Thumbnails" Width="300" Grid.Row="0" Grid.ColumnSpan="3" Orientation="Horizontal" Margin="0,15,0,0">
        </StackPanel>
        <StackPanel Grid.Row="1" Grid.ColumnSpan="3" Orientation="Horizontal" Margin="0,0,0,10">
            <TextBlock Height="50" Width="50" Margin="0,0,4,0" >
            <TextBlock.Background>
            <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ActiveCaptionColorKey}}"/>
            </TextBlock.Background></TextBlock>
           <TextBlock Height="50" Width="50" Margin="0,0,4,0" >
              <TextBlock.Background>
              <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ActiveCaptionColorKey}}"/>
               </TextBlock.Background></TextBlock>
                <TextBlock Height="50" Width="50" Margin="0,0,4,0" >
                <TextBlock.Background>
               <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ActiveCaptionColorKey}}"/>
                </TextBlock.Background></TextBlock>
                <TextBlock Height="50" Width="50" Margin="0,0,4,0" >
                <TextBlock.Background>
                 <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ActiveCaptionColorKey}}"/>
                  </TextBlock.Background></TextBlock>
             <TextBlock Height="50" Width="50" Margin="0,0,4,0" >
              <TextBlock.Background>
                <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ActiveCaptionColorKey}}"/>
                 </TextBlock.Background></TextBlock>
            <TextBlock Height="50" Width="50" Margin="0,0,4,0" >
            <TextBlock.Background>
          <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ActiveCaptionColorKey}}"/>
         </TextBlock.Background></TextBlock>
         </StackPanel>

2 个答案:

答案 0 :(得分:1)

您可以使用WrapPanel而不是StackPanel

答案 1 :(得分:1)

使用UniformGrid并将其Columns属性设置为6:

<UniformGrid Columns="6">
    ...            
</UniformGrid>