2012-10-17 118 views
0

我正在使用WPF的wrappanel。问题是在它的右侧有一个我想减少的空白空间,我不知道如何。保证金在wrappanel

在下面的图片中,您可以看到左侧边缘的右侧,我希望它们都像左侧一样。

enter image description here

这是我的XAML:

<Grid x:Name="root"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="263*" /> 
     <ColumnDefinition Width="240*" /> 
    </Grid.ColumnDefinitions> 
    <Rectangle Fill="LightBlue"/> 
    <WrapPanel > 
     <Rectangle Margin="10" Fill="Red" Width="40" Height="40"></Rectangle> 
     <Rectangle Margin="10" Fill="Red" Width="40" Height="40"></Rectangle> 
     <Rectangle Margin="10" Fill="Red" Width="40" Height="40"></Rectangle> 
     <Rectangle Margin="10" Fill="Red" Width="40" Height="40"></Rectangle> 
     <Rectangle Margin="10" Fill="Red" Width="40" Height="40"></Rectangle> 
     <Rectangle Margin="10" Fill="Red" Width="40" Height="40"></Rectangle> 
    </WrapPanel> 
</Grid> 

回答

1

看起来你忘了居中WrapPanelColumn。像这样:

<Grid x:Name="root"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="263*" /> 
     <ColumnDefinition Width="240*" /> 
    </Grid.ColumnDefinitions> 
    <Rectangle Fill="LightBlue"/> 
    <WrapPanel HorizontalAlignment="Center"> 
     <Rectangle Margin="10" Fill="Red" Width="40" Height="40"></Rectangle> 
     <Rectangle Margin="10" Fill="Red" Width="40" Height="40"></Rectangle> 
     <Rectangle Margin="10" Fill="Red" Width="40" Height="40"></Rectangle> 
     <Rectangle Margin="10" Fill="Red" Width="40" Height="40"></Rectangle> 
     <Rectangle Margin="10" Fill="Red" Width="40" Height="40"></Rectangle> 
     <Rectangle Margin="10" Fill="Red" Width="40" Height="40"></Rectangle> 
    </WrapPanel> 
</Grid> 
0

你在最后得到了很大的空间,因为它只能在你分配的空间中容纳很多方块。它不能完全适合第一行的最后一个方块,因此它将其包裹起来。右侧的那块空间就是额外的“死亡”空间。

你可以用WrapPanel做的另一件事是指定项目的大小。你会看到我已经使用了ItemHeight和ItemWidth属性,这让我更好地控制它的大小。

<Grid x:Name="LayoutRoot"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="263*" /> 
       <ColumnDefinition Width="280*" /> 
      </Grid.ColumnDefinitions> 
      <Rectangle Fill="LightBlue"/> 
      <WrapPanel ItemHeight="60" ItemWidth="60" > 
       <Rectangle Margin="5" Fill="Red" Width="60" Height="60"></Rectangle> 
       <Rectangle Margin="5" Fill="Red" Width="60" Height="60"></Rectangle> 
       <Rectangle Margin="5" Fill="Red" Width="60" Height="60"></Rectangle> 
       <Rectangle Margin="5" Fill="Red" Width="60" Height="60"></Rectangle> 
       <Rectangle Margin="5" Fill="Red" Width="60" Height="60"></Rectangle> 
       <Rectangle Margin="5" Fill="Red" Width="60" Height="60"></Rectangle> 
      </WrapPanel> 
      </Grid>