ListView没有拉伸以适应内容WPF XAML

时间:2018-02-22 15:45:26

标签: wpf xaml listview gridview alignment

嘿所有人我想弄清楚为什么我的内容不会水平拉伸以适合我的ListView项目。我HorizontalContentAlignment设置为伸展,我在第二页上使用了几乎这个精确的布局,内容在第二页上延伸。所以不确定发生了什么以及为什么内容不会延伸。

这是我正在谈论的图片 enter image description here

这是代码

 <Border>
    <Border.Background>
        <ImageBrush ImageSource="/Backgrounds/BlueWaveBackground.jpg"/>
    </Border.Background>
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Center" >
            <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" TextBlock.TextAlignment="Center">
                <Border CornerRadius="10" 
                        Background="{StaticResource ForegroundLightBrush}"
                        Padding="15 10 15 15" 
                        Width="700" 
                        Margin="50 50 50 0">
                    <StackPanel>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <!--First Last Username-->
                            <StackPanel Grid.Column="0" HorizontalAlignment="Center">
                                <TextBlock Text="First Name:" Margin="0 10 10 0" />
                                <TextBlock Text="Last Name:" Margin="0 14 10 0" />
                                <TextBlock Text="Username:" Margin="0 16 10 0"/>
                            </StackPanel>

                            <StackPanel Grid.Column="1" Orientation="Vertical" >
                                <TextBox Grid.Row="0" Text="{Binding SelectedUser.FirstName}"/>
                                <TextBox Text="{Binding SelectedUser.LastName}"/>
                                <TextBox Text="{Binding SelectedUser.UserName}"/>
                            </StackPanel>
                             <!--Email and EmployeeType-->
                            <StackPanel Grid.Column="2" HorizontalAlignment="Center">
                                <TextBlock Text="Email:" Margin="95 10 10 0" />
                                <TextBlock Text="Employee Type:" Margin="10 14 10 0" />
                            </StackPanel>

                            <StackPanel Grid.Column="3" Orientation="Vertical" >
                                <TextBox Grid.Row="0" Text="{Binding SelectedUser.Email}"/>
                                <TextBox Text="{Binding SelectedUser.EmployeeType}"/>
                            </StackPanel>
                        </Grid>
                        <StackPanel Orientation="Horizontal">
                            <Button Content="Add New User" Margin="15"/>
                            <Button Content="Update User" Margin="15"/>
                            <Button Content="Delete User" Margin="15"/>
                            <Button Content="Clear User Info" Margin="15"/>
                        </StackPanel>

                        <!--Data grid-->
                        <ListView ItemsSource="{Binding Users}"
                                  SelectedItem="{Binding SelectedUser}"
                                  Margin="0,20,0,0"  
                                  VerticalAlignment="Stretch" 
                                  HorizontalAlignment="Stretch" 
                                  HorizontalContentAlignment="Stretch"
                                  MaxHeight="150"
                                  x:Name="List">
                            <ListView.View>
                                <GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Users">
                                    <GridViewColumn Header="First Name" Width="Auto" DisplayMemberBinding="{Binding FirstName}"/>
                                    <GridViewColumn Header="Last Name" Width="Auto" DisplayMemberBinding="{Binding LastName}"/>
                                    <GridViewColumn Header="Email" Width="Auto" DisplayMemberBinding="{Binding Email}"/>
                                    <GridViewColumn Header="UserName" Width="Auto" DisplayMemberBinding="{Binding UserName}"/>
                                    <GridViewColumn Header="Employee Type" Width="Auto" DisplayMemberBinding="{Binding EmployeeType}"/>
                                </GridView>
                            </ListView.View>
                        </ListView>
                    </StackPanel>
                </Border>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Border>

感谢您的任何建议!

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。我不得不将边框宽度设置为自动。

<Border CornerRadius="10" 
                    Background="{StaticResource ForegroundLightBrush}"
                    Padding="15 10 15 15" 
                    Width="auto" 
                    Margin="50 50 50 0">
                <StackPanel>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>