在 WPF 中使用带有 foreach 的 ListView 在另一列中添加 file.Name、file.CreationTime

Add file.Name, file.CreationTime in another columns using ListView with foreach in WPF

我已经检查了其他解决方案,但就是无法正常工作!

   DirectoryInfo d = new DirectoryInfo(@leftComboBox.SelectedItem.ToString() + "");
        FileInfo[] files= d.GetFiles("*.*");

        foreach (FileInfo file in files)
        {
            //leftListView.Items.Add(file.Name + " [" + file.CreationTime + "] ");
            var item1 = new ListViewItem(file.Name, file.CreationTime);


        }

我有点迷路了。

      <ListView Name="leftListView" AlternationCount="2"
 Grid.Column="0" Grid.ColumnSpan="4" Margin="10" Background="AliceBlue" BorderBrush="Black" BorderThickness="1" Grid.Row="2" Grid.RowSpan="1"
                 SelectionChanged="leftListView_SelectionChanged"  />

在您的 XAML 文件中

<ListView Name="leftListView" AlternationCount="2" Grid.Column="0" Grid.ColumnSpan="4" Margin="10" Background="AliceBlue" BorderBrush="Black" BorderThickness="1" Grid.Row="2" Grid.RowSpan="1">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Width="500" VerticalAlignment="Center">

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="120" />
                        <ColumnDefinition Width="*" />

                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Column="0" Text="{Binding Name}" />
                    <TextBlock Grid.Column="1" Text="{Binding CreationTime}" />                        
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

在您的代码 .cs 文件中

DirectoryInfo d = new DirectoryInfo(@"D:\Documents\Company");
        FileInfo[] files = d.GetFiles("*.*");
        leftListView.ItemsSource = files;