正在更新绑定进度条

本文关键字:绑定 更新 | 更新日期: 2023-09-27 18:08:22

我试图解决这个问题现在

我有一个ListBox和一个ProgressBar,和一个ObservableCollection结合。现在我下载的东西,并试图更新ProgressBar与:

public delegate void BytesDownloadedEventHandler(ByteArgs e, Item Obj);
public static event BytesDownloadedEventHandler bytesDownloaded;

这里是Event,我从ObservableCollection中得到正确的Obj

private void MainWindow_bytesDownloaded(ByteArgs e,  Item Obj) {
    if (Obj != null) {
        for (int i = 0; i < _downloadList.Count; i++) {
            if (_downloadList[i].Name == Obj.Name) {
                if (_downloadList[i].Value + e.downloaded <= _downloadList[i].Maximum) {
                    App.Current.Dispatcher.Invoke(delegate {
                        _downloadList[i].Minimum = 0;
                        _downloadList[i].Maximum = e.total;
                        _downloadList[i].Value += e.downloaded;
                        lstDowload.ItemsSource = _downloadList;
                    });
                }
                break;
            }
        }
    }
}
这里是ListBox代码和ProgressBar
<ListBox Name="lstDowload" Height="350" Width="707" Visibility="Collapsed" Canvas.Left="-9">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Canvas Height="67" Width="682">
                <Canvas.ToolTip>
                    <ToolTip Name="albumToolTip" Width="Auto" Height="Auto">
                        <Canvas Width="120" Height="150">
                            <Image Source="{Binding ToolTipImage}" Height="152" Width="125" Stretch="Fill"/>
                        </Canvas>
                    </ToolTip>
                </Canvas.ToolTip>
                <ProgressBar Width="288" Canvas.Left="53" Canvas.Top="53" Height="10" Value="{Binding Value}" Maximum="{Binding Maximum}" Minimum="{Binding Minimum}"/>
                <Label Foreground="#FFC3BDBD" FontFamily="{DynamicResource HeaderFontFamily}" FontSize="11" Content="{Binding Name}" Canvas.Left="48" Canvas.Top="15"/>
                <Label Foreground="#FF8D8D8D" FontFamily="{DynamicResource HeaderFontFamily}" FontSize="12" Content="{Binding Status}" Canvas.Left="48" Canvas.Top="30"/>
                <Image Source="{Binding DownloadIcon}" Height="39" Width="49" Canvas.Left="-1" Canvas.Top="6"/>
            </Canvas>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

My Item Properties

public string Name { get; set; }
public Uri DownloadIcon { get; set; }
public int Value {get; set;}
public double Maximum { get; set; }
    public int Minimum { get; set; }

现在我的问题是ProgressBar只是停留在100%,什么也没有发生。也许有人能给我点提示什么的。这对我来说太复杂了:)我尝试了一切,但仍然是一样的:(

正在更新绑定进度条

ObservableCollection中使用的项类型必须实现INotifyPropertyChanged,以便UI注意到这些变化。

也因为你正在使用一个可观察集合你只需要分配ItemsSource一次,实际上你可以绑定到一个属性上的任何对象你分配给你的windows数据环境

例如:

public class Item : INotifyPropertyChanged
{
    private Double value;
    public Double Value
    {
        get { return this.value; }
        set { this.value = value; Changed("Value"); }
    }
    private Double minimum;
    public Double Minimum
    {
        get { return minimum; }
        set { minimum = value; Changed("Minimum"); }
    }
    private Double maximum;
    public Double Maximum
    {
        get { return maximum; }
        set { maximum = value; Changed("Maximum"); }
    }
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; Changed("Name"); }
    }
    private string status;
    public string Status
    {
        get { return status; }
        set { status = value; Changed("Status"); }
    }
    private void Changed(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
下面是用于测试的xaml:
    <ListBox ItemsSource="{Binding Items}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                <ProgressBar Width="288" Height="10" Value="{Binding Value}" Maximum="{Binding Maximum}" Minimum="{Binding Minimum}"/>
                    <Label Foreground="#FFC3BDBD" FontSize="11" Content="{Binding Name}"/>
                    <Label Foreground="#FF8D8D8D" FontSize="12" Content="{Binding Status}" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

和后面的代码…

public partial class MainWindow : Window
{
    Timer timer2 = new Timer(3000);
    Timer timer = new Timer(100);
    Dispatcher gui = Dispatcher.CurrentDispatcher;
    public MainWindow()
    {
        InitializeComponent();
        Items = new ObservableCollection<Item>();
        this.DataContext = this;
        timer.Elapsed += (s, e) =>
        {
            gui.Invoke(() => new Action(() =>
                {
                    foreach (Item item in Items)
                    {
                        if (item.Value < item.Maximum)
                        {
                            item.Value++;
                        }
                    }
                }).Invoke());
        };
        timer.Enabled = true;
        Random rand = new Random();
        timer2.Elapsed += (s, e) =>
            {
                gui.Invoke(() =>                         
                Items.Add(new Item()
                {
                    Minimum = rand.Next(1, 30),
                    Maximum = rand.Next(35, 100),
                }));
            };
        timer2.Enabled = true;
    }
    public ObservableCollection<Item> Items
    {
        get;
        private set;
    }
}