1

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>
Gaston
  • 115
  • 1
  • 7
  • Unless you set the ListView's View property, better use the simpler ListBox base class. – Clemens Dec 08 '21 at 19:49
  • Thanks for the tip. You are of course right. Views are not yet set because they are in the pipeline for later. – Gaston Dec 14 '21 at 13:32

1 Answers1

2

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">
puko
  • 2,819
  • 4
  • 18
  • 27
  • Yes that was it. Great help! I would have searched for a long to find this as I was looking for the problem up to the ItemTemplate level only.. Than You! – Gaston Dec 08 '21 at 19:20