WrapPanel不必要地移动物品

时间:2017-06-16 20:41:43

标签: c# wpf xaml

我有一个ListBox,如果有空间,每行需要显示多个项目。我尝试使用WrapPanel作为常用解决方案,但它将所有项目移动到第一行。这是我正在使用的XAML。

<Border CornerRadius="0.5" Background="#FEFEFE" BorderThickness="0" Margin="20">
        <Border.Effect>
            <DropShadowEffect BlurRadius="4" Color="#000000" Opacity="14" ShadowDepth="0" Direction="0" />
        </Border.Effect>
        <ListBox x:Name="listaAtletas" Background="Transparent" BorderThickness="0" Margin="0" Padding="0" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" IsItemsHost="True" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid MinWidth="350" Height="100">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Image Source="{Binding Imagem}" Width="40" Height="40" Grid.Column="0" />
                        <StackPanel Orientation="Vertical"  Grid.Column="1" >
                            <ContentControl Content="{Binding Nome}"/>
                            <TextBlock Text="{Binding Nascimento}" HorizontalAlignment="Left" />
                            <TextBlock Text="{Binding Status}" HorizontalAlignment="Left" />
                            <TextBlock Text="{Binding Modalidades}" HorizontalAlignment="Left" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal"  Grid.Column="2" >
                            <Button x:Name="EditButton" Content="E" Width="20" Height="20" />
                            <Button x:Name="DeleteButton" Content="X" Width="20" Height="20" />
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Border>

ListBox如果在读取文件后动态填充。这段代码有问题吗?

1 个答案:

答案 0 :(得分:0)

HorizontalScrollBarVisibility属性设置为Disabled而不是Hidden

<ListBox ... ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    ...
</ListBox>