0

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.

8
  • 1
    Have you tried binding an actual collection to the ListView instead of manipulating the ListView.Items directly. Bindins an ObservableCollection<DownloadItem> may behave a lot more reliably Commented Aug 26, 2013 at 21:07
  • @sa_ddam213 No i didn't, I will take a look at what you suggested, But i don't think this is the problem. I have other function that update the Items inside the listview and they do work correctly (UI updated aswell). Commented Aug 26, 2013 at 21:12
  • Have you stuck a breakpoint in the tick handler? Is the handler being called more than once? Is there an exception being thrown somewhere on another thread which is messing things up (ctrl-alt-e in VS, check "Common Language Runtime Exceptions: Thrown")? Commented Aug 26, 2013 at 21:15
  • It could be that the UpdateSourceTrigger is not defaulting to PropertyChanged for int, maybe try {Binding sizeProgress, UpdateSourceTrigger=PropertyChanged}, and it may be a good idea to make sizeProgress a long to match the data outerwise it could overflow. Commented Aug 26, 2013 at 21:17
  • @canton7 yes i have CLR Exceptions being thrown, and the handler is being called every second (tested using simple MessageBox) Commented Aug 26, 2013 at 21:20

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.