width="*" 的网格列没有按预期使用所有可用的 space

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

我第一次使用 ListView 时遇到问题。我的自定义项应该有(当前)2 行 3 列,每行包含一个标签(第一列宽度 =“自动”),一个文本框(填充第二列(测试为 =“” 或宽度 = 100") 和第 3 列中的按钮 (width="Auto")

不幸的是,第二列无法缩放以使用完整的 Listview 宽度,但其行为类似于 width="Auto"。

请注意,最初我在 DataTemplate 中使用 StackPanel 作为顶部控件并将其替换为网格,只是为了检查它是否可以解决问题。

直接在 window dows 中测试测试应用程序中的 DataTemplate 网格按预期工作。

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

我认为问题在于 HorizontalContentAlignmentListView 默认值是 Left

尝试将 HorizontalContentAlignment 设置为 Stretch

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