0

我正在使用 WPF 的包装面板。问题是在它的右侧有一个空白空间,我想减少它,但我不知道如何。

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

在此处输入图像描述

这是我的 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>
4

2 回答 2

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>
于 2012-10-17T14:20:34.960 回答
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>
于 2012-10-17T14:29:46.417 回答