Fr0z3n
Fr0z3n

Reputation: 1576

WPF Timer and UI Update Issue

I have a problem in my application, it does not correctly update the UI after changing a custom property, that is binded to a GridView Column using DisplayMemberBinding="{Binding property}".

XAML:

<ListView x:Name="downloadList" HorizontalAlignment="Left" Height="293" Margin="0,126,0,0" VerticalAlignment="Top" Width="810" Grid.IsSharedSizeScope="True" MouseDoubleClick="DownloadList_MouseDoubleClick">
    <ListView.View>
        <GridView x:Name="DownloadGridView">
            <GridViewColumn x:Name="c_filename" Header="File name" Width="{Binding Source={x:Static p:Settings.Default}, Path=downloadList_fileName_Width, Mode=TwoWay}" DisplayMemberBinding="{Binding fileName}" />
            <GridViewColumn x:Name="c_size" Header="Size" Width="{Binding Source={x:Static p:Settings.Default}, Path=downloadList_size_Width, Mode=TwoWay}" DisplayMemberBinding="{Binding formattedFileSize}" />
            <GridViewColumn x:Name="c_downloaded" Header="Downloaded" Width="{Binding Source={x:Static p:Settings.Default}, Path=downloadList_downloaded_Width, Mode=TwoWay}" DisplayMemberBinding="{Binding sizeProgress}" />
            <GridViewColumn x:Name="c_status" Header="Status" Width="{Binding Source={x:Static p:Settings.Default}, Path=downloadList_status_Width, Mode=TwoWay}" DisplayMemberBinding="{Binding Status}"/>
        </GridView>
    </ListView.View>
</ListView>

This is my custom class with properties:

using System;
using System.Runtime.CompilerServices;
using System.Text;
using System.ComponentModel;

namespace DownloadManager
{
public class DownloadItem : INotifyPropertyChanged
{
    private string _filepath;
    public string filePath
    {
        get { return _filepath; }
        set
        {
            _filepath = value;
            RaisePropertyChanged();
        }
    }

    private int _sizeprogress;
    public int sizeProgress
    {
        get { return _sizeprogress; }
        set
        {
            _sizeprogress = value;
            RaisePropertyChanged();
        }
    }

// and so on...

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(
        [CallerMemberName] string caller = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(caller));
        }
    }

}
}

The timer: Edited to show a real example of what i'm trying to do

System.Windows.Threading.DispatcherTimer updateTimer = new System.Windows.Threading.DispatcherTimer();

updateTimer.Tick += new EventHandler(updateTimer_Tick);
updateTimer.Interval = new TimeSpan(0, 0, 1);


private void updateTimer_Tick(object sender, EventArgs e)
{
    foreach (DownloadItem item in downloadList.Items)
    {
        long BytesReceived = item.filePath.Length;
        item.sizeProgress = BytesReceived;
    }
}

item.filePath contain the Path of a file being downloaded, using a FileStream to write it.

My goal is to read the file size every second and display it.

The problem: The UI, in this case the column binded to sizeProgress, is being Updated only one time, just at the first tick, and then nothing. The app still run without any exception..

And i really do not know what could be the issue.

If you need more Information / Code tell me. Thank you.

Upvotes: 0

Views: 639

Answers (1)

Kent Boogaart
Kent Boogaart

Reputation: 178660

long BytesReceived = item.filePath.Length;

Er, that's the length of the string containing the path to the file, not the length of the file itself.

Upvotes: 1

Related Questions