C# 使用wpf进度条上载多个文件
我正在寻找C#和WPF的解决方案。 我尝试将多个文件上载到服务器。每次上传都应显示在进度条内的列表框中 我有一个WPF列表框模板,其中有一个进度条和一个文本块:C# 使用wpf进度条上载多个文件,c#,wpf,listbox,C#,Wpf,Listbox,我正在寻找C#和WPF的解决方案。 我尝试将多个文件上载到服务器。每次上传都应显示在进度条内的列表框中 我有一个WPF列表框模板,其中有一个进度条和一个文本块: <ListBox Name="lbUploadList" HorizontalContentAlignment="Stretch" Margin="530,201.4,14.2,33.6" Grid.Row="1"> <ListBox.ItemTemplate> <DataTempl
<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;
公共类上载进度
{
公共字符串文件{get;set;}
公共整数百分比{get;set;}
}
列表上传=新建列表();
Add(new UploadProgress(){File=“File.exe”,Percent=13});
Add(newuploadprogress(){File=“test2.txt”,百分比=0});
lbUploadList.ItemsSource=上传;
如何更新此列表中的进度条
有人能帮我找到正确的解决办法吗?:) 您的UploadProgress类必须实现,以便在其值更改时通知绑定 现在,您只需更改列表中某个UploadProgress实例的百分比值,即可更改相应的ProgressBars值 也许您可以创建一个设置百分比值的方法,如:
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's prograssbar.
}
}
我真的不知道上传是如何详细工作的,所以这个方法只是向您展示如何处理百分比值。首先,您需要在类上实现。然后,您应该能够将进度条值绑定到ViewModel,如下所示:
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();
}
}
}
}
我希望这能有所帮助。请注意,您需要.NET Framework 4.5以上版本才能使用
CallerMemberName
。