I spent half a day figuring out why elements in the ListView are not being bind, but I don’t understand how to solve the situation. I work quite a bit with WPF.

<ListView x:Name="lvChildItems" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Comparator.files}" Grid.Row="0" Grid.RowSpan="2" Grid.Column="1"> <ListView.View> <GridView> <GridViewColumn Header="Name" Width="300" DisplayMemberBinding="{Binding Name}"/> </GridView> </ListView.View> </ListView> 

This code does not work. When I replaced Comparator.files with Comparator.Snapshots for interest, it all worked.

Snapshots - List<Snapshot> , files - List<FileP> . The Name property of a Snapshot defined in the class itself, the FileP class inherits it from the base abstract FileOrFolder and therefore (most likely, for others) Binding does not work. How to solve this problem?

UPD
Form Designer:

 public Result() { InitializeComponent(); lvChildItems.ItemsSource = Comparator.files; ... } 

Comparator class

 static class Comparator { public static List<Snapshot> Snapshots = new List<Snapshot>(); public static List<FileP> files = new List<FileP>(); ... } 

Class FileOrFolder

 public abstract class FileOrFolder { public string Name { get; set; } ... } 

Class Folder

 public class Folder : FileOrFolder { public List<FileOrFolder> ChildItems { get; set; } public List<FileP> Files { get { return GetFiles(); } } public List<FileP> GetFiles() { var files = new List<FileP>(); foreach (var item in ChildItems) if (item.GetType() == typeof(FileP)) files.Add((FileP)item); return files; } ... } 

Code setting the value of Comparator.files

 private void tvFiles_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { if (tvFiles.SelectedItem != null) { Comparator.files = ((Folder)((TreeViewItem)tvFiles.SelectedItem).DataContext).Files; lvChildItems.Items.Refresh(); } } 

Code for building a tree

 public static TreeViewItem BuildFSTree(Folder folder) { TreeViewItem root = new TreeViewItem(); root.DataContext = folder; root.Header = folder.Name; root.FontFamily = new FontFamily("Microsoft Sans Serif"); root.FontWeight = FontWeights.Normal; foreach (var item in folder.ChildItems) if (item.GetType() == typeof(FileP)) { root.FontWeight = FontWeights.Bold; break; } if (folder.status != null) { if ((bool)folder.status) root.Foreground = Brushes.Green; else root.Foreground = Brushes.Red; } foreach (FileOrFolder fof in folder.ChildItems) { if (fof.GetType() == typeof(Folder)) root.Items.Add(BuildFSTree((Folder)fof)); } return root; } 

FileP class

 public class FileP : FileOrFolder { public DateTime? LastWriteTime; //public long Size; public FileP(string fullname, DateTime? lastwritetime/*, long size*/) : base(fullname) { LastWriteTime = lastwritetime; //Size = size; } public bool Equals(FileP other) { return FullName == other.FullName && LastWriteTime == other.LastWriteTime; } public static List<FileP> NewFiles(List<FileP> Files1, List<FileP> Files2) { return Files2.Except(Files1, new NewFileP()).ToList(); } public static List<FileP> ChangedFiles(List<FileP> Files1, List<FileP> Files2) { var New = NewFiles(Files1, Files2); var Change = Files2.Except(Files1, new ChangedFileP()).ToList(); return Change.Except(New, new NewFileP()).ToList(); } public override FileOrFolder Copy() { return new FileP(FullName, LastWriteTime/*, Size*/); } } 
  • How do we know why without code? - EvgeniyZ
  • @EvgeniyZ exactly what code is needed? - serjou
  • Well, how do you implement Comparator.files, where the DataContect is set and everything related to the question. binding is not implemented on an empty xaml ... - EvgeniyZ
  • @EvgeniyZ added the code - serjou
  • FileP class? - EvgeniyZ

1 answer 1

As it turned out, inheritance has nothing to do with it. It was impossible to equate the new collection Comparator.files :

 private void tvFiles_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { if (tvFiles.SelectedItem != null) { Comparator.files.Clear(); Comparator.files.AddRange(((Folder)((TreeViewItem)tvFiles.SelectedItem).DataContext).Files); lvChildItems.Items.Refresh(); } }