wrappanel没有正确调整大小

时间:2016-02-15 09:25:51

标签: wpf

我有这样的用户控件

<ListBox x:Class="ColorPickerDemo.Controls.ColourRampListBox"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     Width="auto">

<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel/>
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>

<ListBoxItem>
    <Rectangle x:Name="ColourRectangleYR" Width="85" Height="40" Margin="2">
        <Rectangle.Fill>
            <LinearGradientBrush>
                <GradientStop Color="Yellow" Offset="0"/>
                <GradientStop Color="Red" Offset="1"/>
            </LinearGradientBrush>
        </Rectangle.Fill>
    </Rectangle>
</ListBoxItem>

我在

这样的窗口中使用
  <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="350"/>
        <RowDefinition Height="50*"/>
    </Grid.RowDefinitions>      

    <GroupBox Header="Preset Colour Gradients" Grid.Column="0">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="350"/>
                <ColumnDefinition Width="350"/>
            </Grid.ColumnDefinitions>
            <local:ColourRampListBox Grid.Column="0" />
        </Grid>
    </GroupBox>  
</Grid>

但我得到的输出就像

一样

this

但我想要这个

enter image description here

我无法弄清楚出了什么问题。 请帮助。

谢谢!

2 个答案:

答案 0 :(得分:2)

对于wrappanel,必须指定maxwidth,在这种情况下是包含它的ListBox的宽度。

E.g。

<WrapPanel MaxWidth="300"/>

答案 1 :(得分:2)

Width的{​​{1}}未固定,因此会扩展到无限。 :)

请尝试以下操作: (适用于动态宽度)

Panel