Application like "Explorer": on the left TreeView, and by clicking on the directory on the left in the ListView should display the file name, extension and size. I stumbled upon some example in the internet, tried to do it for my own, but the data stubbornly did not want to be displayed. Help me please.

using System; using System.IO; using System.Windows; using System.Windows.Controls; namespace lab1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private object dummyNode = null; public string SelectedImagePath { get; set; } private void Window_Loaded(object sender, RoutedEventArgs e) { foreach (string s in Directory.GetLogicalDrives()) { TreeViewItem item = new TreeViewItem(); item.Header = s; item.Tag = s; item.FontWeight = FontWeights.Normal; item.Items.Add(dummyNode); item.Expanded += new RoutedEventHandler(folder_Expanded); folders.Items.Add(item); } } void folder_Expanded(object sender, RoutedEventArgs e) { TreeViewItem item = (TreeViewItem)sender; if (item.Items.Count == 1 && item.Items[0] == dummyNode) { item.Items.Clear(); try { foreach (string s in Directory.GetDirectories(item.Tag.ToString())) { TreeViewItem subitem = new TreeViewItem(); subitem.Header = s.Substring(s.LastIndexOf("\\") + 1); subitem.Tag = s; subitem.FontWeight = FontWeights.Normal; subitem.Items.Add(dummyNode); subitem.Expanded += new RoutedEventHandler(folder_Expanded); item.Items.Add(subitem); } } catch (Exception) { } } } private void folders_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { TreeView tree = (TreeView)sender; TreeViewItem temp = ((TreeViewItem)tree.SelectedItem); if (temp == null) return; SelectedImagePath = ""; string temp1 = ""; string temp2 = ""; while (true) { temp1 = temp.Header.ToString(); if (temp1.Contains(@"\")) { temp2 = ""; } SelectedImagePath = temp1 + temp2 + SelectedImagePath; if (temp.Parent.GetType().Equals(typeof(TreeView))) { break; } temp = ((TreeViewItem)temp.Parent); temp2 = @"\"; } try { DirectoryInfo dir = new DirectoryInfo(SelectedImagePath); FileInfo[] content = dir.GetFiles(); foreach (FileInfo f in content) { MyFile file = new MyFile(); file.Name = f.Name; file.Extension = f.Extension; file.Length = f.Length.ToString(); ((System.Collections.ArrayList)files.Resources["fileArray"]).Add(file); } } catch (Exception) { } } } } <Window x:Class="lab1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:col="clr-namespace:System.Collections;assembly=mscorlib" xmlns:local="clr-namespace:lab1" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> <Grid> <Grid.RowDefinitions> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TreeView Grid.Column="0" x:Name="folders" SelectedItemChanged="folders_SelectedItemChanged" Width="Auto" Background="#FFFFFFFF" BorderBrush="#FFFFFFFF" Foreground="#FFFFFFFF"/> <ListView Grid.Column="1" x:Name="files" ItemsSource="{DynamicResource ResourceKey='fileArray'}"> <ListView.View> <GridView> <GridViewColumn Header="Name" Width="70" DisplayMemberBinding="{Binding Path=fileName}"></GridViewColumn> <GridViewColumn Header="Type" Width="70" DisplayMemberBinding="{Binding Path=fileExtension}"></GridViewColumn> <GridViewColumn Header="Size" Width="70" DisplayMemberBinding="{Binding Path=fileLength}"></GridViewColumn> </GridView> </ListView.View> <ListView.Resources> <col:ArrayList x:Key="fileArray"></col:ArrayList> </ListView.Resources> </ListView> </Grid> </Window> namespace lab1 { class MyFile { public string Name { get; set; } public string Extension { get; set; } public string Length { get; set; } } } 
  • Soooo ... what's the question? What does not work? What is the result? That does not work? :) - Denis Bubnov
  • I click on the directory, but the information about the contained files is not displayed. They are taken correctly, but are not transmitted to the ListView in any way. WPF is completely new, so I don’t know how to coordinate the work of C # and XAML: '( - guitarhero

0