Upload some files with wpf progress steps

I am looking for a solution in C # and WPF. I am trying to upload multiple files to a server. Each download should be listed in the progress bar.

I have a WPF list template with a progress bar and a text block:

<ListBox Name="lbUploadList" HorizontalContentAlignment="Stretch" Margin="530,201.4,14.2,33.6" Grid.Row="1"> <ListBox.ItemTemplate> <DataTemplate> <Grid Margin="0,2"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="100" /> </Grid.ColumnDefinitions> <TextBlock Text="{Binding File}" /> <ProgressBar Grid.Column="1" Minimum="0" Maximum="100" Value="{Binding Percent}" /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> public class UploadProgress { public string File { get; set; } public int Percent { get; set; } } List<UploadProgress> uploads = new List<UploadProgress>(); uploads.Add(new UploadProgress() { File = "File.exe", Percent = 13 }); uploads.Add(new UploadProgress() { File = "test2.txt", Percent = 0 }); lbUploadList.ItemsSource = uploads; 

How can I update the progress bar on this list?

Can someone help me find the right solution? :)

+4
source share
2 answers

First, you will need to implement the INotfyPropertyChanged interface in your class. Then you should be able to bind the progress bar value to the ViewModel like this:

 public class UploadProgress : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; // This method is called by the Set accessor of each property. // The CallerMemberName attribute that is applied to the optional propertyName // parameter causes the property name of the caller to be substituted as an argument. private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } private int percent = 0; public int Percent { get { return percent; } set { if (value != percent) { percent = value; NotifyPropertyChanged(); } } } } 

Hope this helps.

+3
source

Your UploadProgress class must implement INotifyPropertyChanged to notify the binding when the value changes.

Now you just need to change the percentage value of some instance of the UploadProgress instance in your list to change the corresponding value of ProgressBars.

Perhaps you are creating a method that sets the Percentage value, for example:

 private void Upload(UploadProgress upload) { byte[] uploadBytes = File.GetBytes(upload.File); step = 100/uploadBytes.Length; foreach (byte b in uploadBytes) { UploadByte(b); upload.Percent += step; //after you implemented INotifyPropertyChanged correctly this line will automatically update it prograssbar. } } 

I really don't know how loading works, so this method just shows you how you could handle the percentage value.

0
source

Source: https://habr.com/ru/post/1502009/


All Articles