Grid column with width="*" does not use all available space as expected

428 Views Asked by At

I have a problem with my first ListView usage. My custom item should have (currently) 2 rows with 3 columns each containing a label (1st column width="Auto"), a textbox (fill-up 2nd column (tested with="" or width="100") and a button in the 3rd column (width="Auto")

Unfortunately the second column does not scale to use full Listview width but behaves like width="Auto".

Note that initially I used a StackPanel as top control in the DataTemplate and replaced it by a grid, just to check if it could solve the problem.

Testing the DataTemplate Grid in a test application directely in a window dows work as expected.

              <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Label Content="Input Paths"/>
                <ListView Grid.Row="1" ItemsSource="{Binding PathListAccess.PathList.PathList}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <!--Row 0-->
                                    <Label Grid.Row="0" Grid.Column="0" Content="Input Directory:"/>
                                    <TextBox Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" Text="{Binding InputPath}"/>
                                    <Button Grid.Row="0" Grid.Column="2" Content="..."/>
                                    <!--Row 1-->
                                    <Label Grid.Row="1" Grid.Column="0" Content="Output Directory:"/>
                                    <TextBox Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" Text="{Binding OutputPath}"/>
                                </Grid>
                            </Grid>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Grid>
1

There are 1 best solutions below

1
puko On BEST ANSWER

I think the problem is that the ListView default value for HorizontalContentAlignment is Left.

Try to set HorizontalContentAlignment to Stretch

<ListView Grid.Row="1" ItemsSource="{Binding PathListAccess.PathList.PathList}"
          HorizontalContentAlignment="Stretch">