Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView not stretching to fit content WPF XAML

Hey Everyone I am trying to figure out why my content will not stretch horizontally to fit my ListView items. I have HorizontalContentAlignment set to stretch, I used almost this exact layout on a second page and the content stretches on the second page. So not exactly sure what is going on and why the content won't stretch.

Here is a picture of what I am talking about enter image description here

here is the code

 <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>

Thanks for any suggestions!

like image 592
Eric Obermuller Avatar asked Oct 17 '22 22:10

Eric Obermuller


1 Answers

I found the solution. I had to set my border width to auto.

<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"/>
like image 92
Eric Obermuller Avatar answered Oct 21 '22 01:10

Eric Obermuller