For problems with using ProgressBar to display the download process in wpf, you could try to refer to the code below.
Xaml:
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<DataGrid AutoGenerateColumns="False" x:Name="servers" ItemsSource="{Binding View}" CanUserAddRows="False"
HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Column="1" >
<DataGrid.Columns>
<DataGridTextColumn Header="ID" IsReadOnly="True" Binding="{Binding Id}" Width="50"/>
<DataGridTextColumn Header="Text" IsReadOnly="True" Binding="{Binding Text}" Width="50"/>
<DataGridTemplateColumn Header="Progress" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ProgressBar x:Name="progressbar" Maximum="100" Width="140" Height="20" Value="{Binding Progress}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="PValue" IsReadOnly="True" Width="50" Binding="{Binding ProgressValue}"/>
<DataGridTemplateColumn Header="Action" Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<Button Command="{Binding}">
<Image Height="50" Width="50" Source="down.jfif"></Image>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Codebehind:
public class ViewModel
{
CollectionViewSource cvs = new CollectionViewSource();
public ICollectionView View { get => cvs.View; }
public ObservableCollection<Clients> MyClients;
public ViewModel()
{
cvs.Source = GetData();
}
private ObservableCollection<Clients> GetData()
{
ObservableCollection<Clients> MyClients = new ObservableCollection<Clients>();
MyClients.Add(new Clients() { Id = 1, Text = "https://sample-videos.com/doc/Sample-doc-file-1000kb.doc" });
MyClients.Add(new Clients() { Id = 3, Text = "https://freetestdata.com/wp-content/uploads/2021/09/1-MB-DOC.doc" });
return MyClients;
}
}
public class Clients : ICommand, INotifyPropertyChanged
{
public Clients() => disp = Dispatcher.CurrentDispatcher;
private Dispatcher disp;
public int Id { get; set; }
public string Text { get; set; }
public int Status { get; set; }
public string ProgressValue { get; set; }
WebClient client1 = new WebClient();
public void Execute(object parameter)
{
if (client1.IsBusy) return;
Progress = 0;
Uri uri = new Uri(Text);
String FileName = System.IO.Path.GetFileName(uri.AbsolutePath);
client1.DownloadFileAsync(uri, @"C:\Users\Administrator\Desktop\" + FileName);
client1.DownloadProgressChanged += downloadprogress;
client1.DownloadFileCompleted += Client_DownloadFileCompleted;
}
public double Progress { get; set; }
private void downloadprogress(object sender, DownloadProgressChangedEventArgs e)
{
Progress = e.ProgressPercentage;
double receive = double.Parse(e.BytesReceived.ToString());
double total = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = receive / total * 100;
ProgressValue = $"{string.Format("{0:0.##}", percentage)}%";
Status = int.Parse(Math.Truncate(percentage).ToString());
OnPropertyChanged(nameof(Progress));
OnPropertyChanged(nameof(ProgressValue));
OnPropertyChanged(nameof(Status));
}
private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
System.Windows.Forms.MessageBox.Show("Download OK!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => true;
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
The result:
-
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.