WPF表单的布局 - 水平列表框

时间:2013-10-16 09:40:38

标签: c# wpf xaml

我需要并排显示多个列表框,但我不知道任何时候会有多少列表框。我已经将其作为一个网站,但已决定转向桌面解决方案,因此需要从网站重新创建布局。

这就是我想要的: enter image description here

这就是我在WPF中所拥有的 enter image description here

这是我当前的XAML文件。

<Grid>
    <StackPanel Height="auto" Width="auto">
        <StackPanel Orientation="Horizontal">
            <WrapPanel Orientation="Horizontal">
                <ListBox ItemsSource="{Binding BoardMarker.Events}" Height="auto" Width="auto" Padding="-6,-3,0,-5">
                    <ListBox.ItemTemplate>
                        <HierarchicalDataTemplate>
                            <StackPanel>
                                <Label Foreground="White" FontWeight="Bold" Content="{Binding Name}" >
                                    <Label.Background>
                                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                            <GradientStop Color="Black" Offset="0"/>
                                            <GradientStop Color="#FF7A7A7A" Offset="1"/>
                                        </LinearGradientBrush>
                                    </Label.Background>
                                </Label>
                                <ListBox ItemsSource="{Binding Markets}" Padding="-6,-5,-6,-10">
                                    <ListBox.ItemTemplate>
                                        <HierarchicalDataTemplate>
                                            <StackPanel Width="auto">
                                                <Grid MinWidth="120" Background="DodgerBlue">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="55*"/>
                                                        <ColumnDefinition Width="45*"/>
                                                    </Grid.ColumnDefinitions>
                                                    <Label Content="{Binding Name}" HorizontalAlignment="left" FontSize="8" Padding="0,2" Foreground="White" FontWeight="Bold"/>
                                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Background="DodgerBlue" Margin="0,0,2,0" Grid.Column="1">
                                                        <Label Content="{Binding NumberOfRunners}" FontSize="7" ClipToBounds="False" SnapsToDevicePixels="True" Padding="0,2" Foreground="White" FontWeight="Bold"/>
                                                        <Label HorizontalAlignment="Right"  Content="Run - " FontSize="7" Padding="0,2" Foreground="White" FontWeight="Bold"/>
                                                        <Label Content="{Binding NumberOfNonRunners}" FontSize="7" Padding="0,2" Foreground="White" FontWeight="Bold"/>
                                                        <Label Content="NR" FontSize="7" Padding="0,2" Foreground="White" FontWeight="Bold"/>
                                                    </StackPanel>
                                                </Grid>

                                                <ListBox ItemsSource="{Binding Selections}" Width="auto" Padding="-2">
                                                    <ListBox.ItemTemplate>
                                                        <HierarchicalDataTemplate>
                                                            <Grid Width="auto" MinWidth="120" Margin="-2">
                                                                <Grid.ColumnDefinitions>
                                                                    <ColumnDefinition Width="10*"/>
                                                                    <ColumnDefinition Width="70*"/>
                                                                    <ColumnDefinition Width="15*"/>
                                                                    <ColumnDefinition Width="15*"/>
                                                                </Grid.ColumnDefinitions>
                                                                <Label Content="{Binding Number}" FontSize="7" Padding="2"/>
                                                                <Label Content="{Binding Name}" FontSize="7" Padding="2" Grid.Column="1" />
                                                                <Label Content="{Binding CurrentPrice}" FontSize="7" Padding="2" HorizontalAlignment="Right" Grid.Column="2"/>
                                                                <Label Foreground="DarkGray" Content="{Binding OpeningPrice}" FontSize="7" Padding="2" HorizontalAlignment="Right" Grid.Column="3"/>
                                                            </Grid>
                                                        </HierarchicalDataTemplate>
                                                    </ListBox.ItemTemplate>
                                                </ListBox>
                                            </StackPanel>
                                        </HierarchicalDataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>
                            </StackPanel>
                        </HierarchicalDataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </WrapPanel>
        </StackPanel>
    </StackPanel>
</Grid>

1 个答案:

答案 0 :(得分:0)

您的内部StackPanel具有默认方向,即Vertical。将其设置为水平。如果希望它们向左运行,您可能需要考虑具有水平方向的WrapPanel - &gt;对,然后到下一行。