Duaplicate in lisview

Eduardo Gomez 3,671 Reputation points
2022-06-27T17:57:17.177+00:00

I have a ListView, and I am trying to eliminates duplicates.

  public Command? SendCommand { get; set; }  
  
        public Command? ValidateTextCommand { get; set; }  
  
        public Command? ValidateNumberCommand { get; set; }  
  
        public Command? SearchPictureCommand { get; set; }  
  
        public Command<Window>? DragWindowCommand { get; set; }  
  
        public Building? Building { get; set; }  
  
        public File? File { get; set; }  
  
        public string? SeletedItem { get; set; }  
  
        public PrpertiesWindowViewModel() {  
  
            Building = new Building();  
  
            SendCommand = new Command(SendToServer);  
  
            ValidateTextCommand = new Command(ValidateTextAction);  
  
            ValidateNumberCommand = new Command(ValidateNumberAction);  
  
            SearchPictureCommand = new Command(SearchPictureAction);  
  
            DragWindowCommand = new Command<Window>(DragWindowAction);  
        }  
  
        private void DragWindowAction(Window obj) {  
            if (obj != null) {  
                obj.DragMove();  
            }  
        }  
  
        private void SearchPictureAction() {  
            var dialog = new OpenFileDialog {  
                Filter = "Image files (*.png;*.jpeg)|*.png;*.jpeg;",  
                CheckPathExists = true,  
                Multiselect = true,  
                InitialDirectory = GetFolderPath(SpecialFolder.MyPictures)  
            };  
  
            if (dialog.ShowDialog() == true) {  
  
                if (Building?.FileNames != null) {  
  
                    foreach (var item in dialog.FileNames) {  
  
                        File = new File { FileName = Path.GetFileName(item), FullPath = Path.GetFullPath(item) };  
  
                        if (!Building.FileNames.Contains(File)) {  
  
                            Building.FileNames.Add(File);  
                        }  
                    }  
                }  
            }  
        }  

If a file, does not exist in my collection, add it.

class

public string? Name { get; set; }  
  
        public string? Desc { get; set; }  
  
        public string? Address { get; set; }  
  
        public string? Rooms { get; set; }  
  
        public ObservableCollection<File>? FileNames { get; set; }  
          
        public Building() {  
            FileNames = new ObservableCollection<File>();  
        }  
    }  
  
    [AddINotifyPropertyChangedInterface]  
    public class File {  
  
        public string? FullPath { get; set; }  
  
        public string? FileName { get; set; }  
    }  
}  
  
Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2022-06-28T08:05:07.5+00:00

    Hi,@Eduardo Gomez . Welcome Microsoft Q&A.

    You could use the following code to determine if File.FullPath exists in FileNames to add a new Item.

    Code:
    215610-13.txt
    215943-image.png

    The result:
    02. png is not added to the collection repeatedly when added again.
    215617-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.