更新绑定的Progressbar

时间:2015-08-18 08:10:01

标签: c# wpf

Hej我现在试图解决这个问题。

我有ListBox ProgressBar,绑定到ObservableCollection。现在我正在下载内容并尝试使用:

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

来自Event

Obj我正确的ObservableCollection
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>

我的物品属性

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%并且没有任何反应。 也许有人可以给我一些提示或其他东西。这对我来说很复杂:)即时尝试一切但仍然相同:(

1 个答案:

答案 0 :(得分:3)

ObservableCollection中使用的项类型必须为UI实现INotifyPropertyChanged以记录更改。

此外,由于您使用了一个可观察的集合,您只需要分配一次ItemsSource,实际上您可以将它绑定到您指定的任何对象上的属性作为您的Windows Datacontext

类似的东西:

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