c# - 使用 wpf 进度条上传多个文件

标签 c# wpf listbox

我正在寻找 C# 和 WPF 中的解决方案。 我尝试将多个文件上传到服务器。每次上传都应显示在进度条内的列表框中。

我有一个带有进度条和文本 block 的 WPF 列表框模板:

<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;

如何更新此列表中的进度条?

有人可以帮我找到正确的解决方案吗? :)

最佳答案

首先你需要实现INotfyPropertyChanged interface在你的课上。然后您应该能够像这样将进度条值绑定(bind)到 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();
            }
        }
    }
}

希望对您有所帮助。

关于c# - 使用 wpf 进度条上传多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18781006/

相关文章:

c# - 仅当下一个字符为小写时才将字符串拆分为大写

c# - C++/CLI Dll 与 C# 连接问题

c# - NUnit:如何从非静态方法传递 TestCaseData?

c# - 为什么 string.StartsWith ("\u2D2D") 总是返回 true?

wpf - 风格错误?无法在Windows10中设置样式内的背景颜色

wpf - 救命 ”?”纽扣

c# - 具有 Entity Framework 的 Wpf 应用程序无法在其他系统上运行。显示错误 50

c# - MVC - 将所有列表框项目传递回 Controller

wpf - 当鼠标悬停在任何内容上时,如何使ScrollViewer滚动

WPF - 将列表框绑定(bind)到图像的 ObservableCollection