Wpf列表框大于窗口

时间:2016-03-22 18:56:31

标签: c# wpf xaml listbox

我正在学习wpf / c#,并应用我所学到的关于不同的wpf布局。我试图构建一个简单的员工列表/详细信息窗口,但我有一点问题:

列表框高度大于窗口的高度,而不是添加滚动条,其余元素未显示(某些元素显示为一半)

这是一张图片:

enter image description here

这是xaml:

<Window x:Class="elexim.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:elexim"
        mc:Ignorable="d"
        Title="MainWindow">

    <StackPanel Orientation="Vertical">
        <Label HorizontalAlignment="Center">Employés </Label>
        <Separator Margin="20,0,0,0"/>

        <Grid Margin="0,20,0,0">

            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <GroupBox Header="Liste Employés" Margin="20,0,0,0">

                <ListBox>
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Columns="3"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>

                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                    <local:employeeInfo/>
                </ListBox>
            </GroupBox>

            <GroupBox Header="Detail Employé" Margin="20,0,0,0" Grid.Column="1">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>

                    <Label Margin="10">Nom</Label>
                    <TextBox Grid.Column="1" VerticalAlignment="Center"/>

                    <Label Grid.Column="0" Grid.Row="1" Margin="10">Age</Label>
                    <TextBox Grid.Row="1" Grid.Column="1" VerticalAlignment="Center"/>

                    <Label Grid.Column="0" Grid.Row="2" Margin="10">Salaire</Label>
                    <TextBox Grid.Row="2" Grid.Column="1" VerticalAlignment="Center"/>

                    <StackPanel Orientation="Horizontal" Grid.Column="0" 
                                Grid.Row="3" Grid.ColumnSpan="2"
                                HorizontalAlignment="Right">
                        <Button Margin="10" MinWidth="100">Valider</Button>
                        <Button Margin="10" MinWidth="100">Annuler</Button>
                    </StackPanel> 

                </Grid>


            </GroupBox>
        </Grid>

    </StackPanel>

</Window>

如何让列表框添加滚动条而不是当前效果?

1 个答案:

答案 0 :(得分:2)

只需使用Grid的{​​{1}}:

让我举个例子:

StackPanel

或将<Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Label HorizontalAlignment="Center">Employés </Label> <Separator Grid.Row="1" Margin="20,0,0,0"/> <Grid Grid.Row="2" Margin="0,20,0,0"> <Grid.RowDefinitions> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <GroupBox Header="Liste Employés" Margin="20,0,0,0"> <ListBox> <ListBox.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Columns="3"/> </ItemsPanelTemplate> </ListBox.ItemsPanel> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> <local:employeeInfo/> </ListBox> </GroupBox> <GroupBox Header="Detail Employé" Margin="20,0,0,0" Grid.Column="1"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Label Margin="10">Nom</Label> <TextBox Grid.Column="1" VerticalAlignment="Center"/> <Label Grid.Column="0" Grid.Row="1" Margin="10">Age</Label> <TextBox Grid.Row="1" Grid.Column="1" VerticalAlignment="Center"/> <Label Grid.Column="0" Grid.Row="2" Margin="10">Salaire</Label> <TextBox Grid.Row="2" Grid.Column="1" VerticalAlignment="Center"/> <StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" HorizontalAlignment="Right"> <Button Margin="10" MinWidth="100">Valider</Button> <Button Margin="10" MinWidth="100">Annuler</Button> </StackPanel> </Grid> </GroupBox> </Grid> </Grid> 放在StackPanel内:

ScrollViewer