How do I recursively go through all my items and update/show their sizes?

Dani_S 4,501 Reputation points
2023-10-09T06:36:10.2333333+00:00

Hi,

Assume I have tree view(third party) that show the following model.

how do I recursively go through all my items and update/show their sizes(length) in entry?

Thanks,

 public class UploadItem:BaseViewModel
    {
        public UploadItem(string name)
        {
            Name = name;

            Children = new ObservableCollection<UploadItem>();
        }

        public string Name { get; set; }
        public string Path { get; set; }
        public string Icon { get; set; }
        public IList<UploadItem> Children { get; set; }

        public string IconActionListView { get; set; }
        public Action IconAction { get; set; }
        public Command IconClickCommand => new Command(IconClicked);
        private void IconClicked()
        {
            this.IconAction?.Invoke();
        }

        public long  Length { get; set; }

        public string CreatedDateTime { get; set; }
        public string ModifiedDateTime { get; set; }
        public string FileType { get; set; }

        public override string ToString()
        {   
     
            if (Icon == "file")
            {
                return $"Size:{(Length / 1024f) / 1024f} Mega, created at: {CreatedDateTime}, modified at: {ModifiedDateTime}, File type: {FileType} ";
            }
           

            return string.Empty;
        }
    }
Developer technologies | .NET | .NET MAUI
{count} votes

2 answers

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-10-10T08:28:19.77+00:00

    Hello,

    For your needs, you could mainly divide into how to recursively update the values in an array and how to update the updated values into the control.

    For how to update recursively, please refer to the following ViewModel:

    public class MyViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string name = "") =>
       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        public MyViewModel()
        {
            Item = new UploadItem("test");
        }
        private UploadItem item;
        public Command IconClickCommand => new Command(Update_element_recursive);
        public UploadItem Item
        {
            get { return item; }
            set
            {
                if (item != value)
                {
                    item = value;
                    OnPropertyChanged();
                }
            }
        }
    
        private void Update_element_recursive()
        {
            update_element_recursive(this.Item.Children.Count);
        }
        private void update_element_recursive(int size)
        {
            if (size - 1 >= 0) //This represents a recursive exit condition
            {
                update_element_recursive(size - 1);
            }
            else
            {
                return;
            }
            Item.Children[size - 1].Length = 1024; //You can update the length value according to your needs
        }
    }
    

    For how to make the control respond to changes in the length value, you need to add the OnPropertyChanged event in the Model layer.

    public class UploadItem : INotifyPropertyChanged
    {
    
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string name = "") =>
       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        public UploadItem()
        {
        }
    
        public UploadItem(string name)
        {
            Name = name;
    
            Children = new ObservableCollection<UploadItem>();
            Children.Add(new UploadItem { Length = 123 });
            Children.Add(new UploadItem { Length = 123 });
            Children.Add(new UploadItem { Length = 123 });
            Children.Add(new UploadItem { Length = 123 });
        }
    
        public string Name { get; set; }
        public string Path { get; set; }
        public string Icon { get; set; }
    
        public IList<UploadItem> Children { get; set; }
    
        public string IconActionListView { get; set; }
        public Action IconAction { get; set; }
    
        private void IconClicked()
        {
            this.IconAction?.Invoke();
        }
        private long length;
        public long Length
        {
            get { return length; }
            set
            {
                if (length != value)
                {
                    length = value;
                    OnPropertyChanged();
                }
            }
        }
    
        public string CreatedDateTime { get; set; }
        public string ModifiedDateTime { get; set; }
        public string FileType { get; set; }
    
        public override string ToString()
        {
    
            if (Icon == "file")
            {
                return $"Size:{(Length / 1024f) / 1024f} Mega, created at: {CreatedDateTime}, modified at: {ModifiedDateTime}, File type: {FileType} ";
            }
    
    
            return string.Empty;
        }
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


  2. Dani_S 4,501 Reputation points
    2023-10-16T04:51:11.3666667+00:00

    Hi,

    Finally i wrote function in viewmodel:

    private void ShowTotalSizeOfFiles()
            {
               
    
                long x = 0;
                foreach (var item in CheckedItems)
                {
                    if(item.Icon == "drive" || item.Icon == "folder")
                    {
                        continue;
                    }
    
                     x += item.Length;
                }
                Lenght = x;// property that bound to view
            }
    
    0 comments No comments

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.