Reputation: 621
Im currently coding a Downloader. Now i need to make a Total ProgressBar. But i have no idea how to, i know that i need to calculate the Total Download Size.
Since the Downloads are in a List i could maybe some how bind the progressbar to the Count property of the List. Or is there a better Way of doing that. Heres the Code for the Song ProgressBars.
Sorry for my bad english!
private void MainWindow_bytesDownloaded(ByteArgs e, Song songObj) {
if (songObj != null) {
gui.Invoke(() => new Action(() => {
EditState(State.DOWNLOADING, songObj);
foreach (var item in _downloadList) {
if (item.SongName == songObj.SongName) {
item.Maximum = (int)e.total;
item.Value += (int)e.downloaded;
AllMaximum += e.total;
break;
}
}
}).Invoke());
}
}
Upvotes: 0
Views: 641
Reputation: 13357
It's a ratio.
You know the full length of the bar.
You know the progress of the download (in bytes).
You know the full length of the download (in bytes).
1) For a single download progress... Solve for x:
x/(full_bar_length) = (current_progress)/(full download length)<br>
x = ((current_progress)/(full download length)) * full_bar_length
Then bind the length of the progress to x.
2) For the case of "Grand total:"
Sum the total bytes across all downloads.
Sum the current bytes downloaded across all downloads.
Solve for x:
x/(full_bar_length) = (sum_current_bytes)/(total_bytes_sum)<br>
x = ((sum_current_bytes)/(total_bytes_sum)) * (full_bar_length)
Again, bind x to your "grand total current progress".
As far as the UI goes, the List (_downloadList) is bound to the ItemsSource of an ItemsControl:
<ItemsControl ItemsSource="{Binding _downloadList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="10">
<Rectangle Fill="Green" Width="{Binding RatioValue}" Height="50" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I haven't tried that, but it should be close to what you are asking for -- how to fill a rectangle based on an Item's Value, for each item.
You'll need to add a new property to your Download class, and calculate a RatioValue, based on the Value, and have that be in the item, where the Value property is.
Upvotes: 1