I am developing a Download Manager. In which i am add files list in listview. in listview i have added progressbar. but how can i update its value.
Value should be updated as the file downloads.
This is my MainWindow.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Net;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DownloadManager
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
List<string> Namelist = new List<string>();
public MainWindow()
{
InitializeComponent();
}
private void Add_MouseUp(object sender, MouseButtonEventArgs e)
{
Add_URL au = new Add_URL();
au.ShowDialog();
}
WebClient client1 = new WebClient();
public void download(Add_URL a, String Text)
{
Uri uri = new Uri(Text);
String FileName = System.IO.Path.GetFileName(uri.AbsolutePath);
client1.DownloadFileAsync(uri, "E:/" + FileName);
client1.DownloadProgressChanged += downloadprogress;
}
private void Pause_MouseUp(object sender, MouseButtonEventArgs e)
{
// client = new WebClient();
client1.CancelAsync();
// MessageBoxResult result = MessageBox.Show("Pause");
}
Double a1;
public class MyItem
{
public String FileName { get; set; }
// public string Progressbar { get; set; }
MainWindow o = new MainWindow();
public string Size { get; set; }
public string Time { get; set; }
private double _progress;
public double Progress
{
get { return _progress; }
set { _progress =o.a1; NotifyPropertyChanged("Progress"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName = "Progress")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public double percent
{
get;
set;
}
}
String filename;
public void AddItem(Add_URL a, String Filename)
{
filename = Filename;
this.FilesList.Items.Add(new MyItem { FileName = "Engineering", Progress= a1, Size ="100",Time="10.30 PM" });
// NoFiles.Visibility = Visibility.Hidden;
// NoFileImage.Visibility = Visibility.Hidden;
}
public double Percentag { get; set; }
double Percentage;
public void downloadprogress(object sender, DownloadProgressChangedEventArgs e)
{
pb.Minimum = 0;
double receive = double.Parse(e.BytesReceived.ToString());
double Filesize = double.Parse(e.TotalBytesToReceive.ToString());
Percentage = receive / Filesize* 100;
a1= int.Parse(Math.Truncate(Percentage).ToString());
pb.Value= int.Parse(Math.Truncate(Percentage).ToString());
//pb1.Value = pb.Value;
// MyItem o = new MyItem();
//o.percent = pb.Value;
}
private void Resume_MouseUp(object sender, MouseButtonEventArgs e)
{
MessageBoxResult result = MessageBox.Show("Resume");
}
private void Delete_MouseUp(object sender, MouseButtonEventArgs e)
{
FilesList.Items.RemoveAt(FilesList.SelectedIndex);
// NoFileImage.Visibility=System.Windows.Visibility.Hidden;
}
// public void hide()
//{
// this.ShowActivated = false;
// NoFileImage.Visibility = Visibility.Hidden;
// MessageBoxResult result = MessageBox.Show("Delete");
//}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
}
private void Search_MouseUp(object sender, MouseButtonEventArgs e)
{
// Search search = new Search();
String name2 = "En";
// search.ShowDialog();
// private ListView FilesList = new ListView();
foreach (ListViewItem item in FilesList.Items)
{
if (!item.ToString().ToLower().Contains(name2.ToLower()))
{
FilesList.Items.Remove(item);
}
}
}
private void ChangePath_MouseUp(object sender, MouseButtonEventArgs e)
{
ChangePath cp = new ChangePath();
cp.ShowDialog();
}
private void Button_Click(object sender, EventArgs e)
{
String name2 = "En";
int i = 0;
foreach (Object item in FilesList.Items)
{
i++;
if (!item.ToString().ToLower().Contains(name2.ToLower()))
{
MessageBoxResult result = MessageBox.Show(name2+ " Item Found at "+i+" Line");
//FilesList.Items.Remove(item);
}
else
{
MessageBoxResult result = MessageBox.Show("No Item Found");
}
}
}
}
}
and this is my XAML code
<GridViewColumn.CellTemplate >
<DataTemplate >
<ProgressBar x:Name="progressbar" Maximum="100" Width="140" Value="{Binding Path=Progress}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>