WPF计时器和UI更新问题

时间:2013-08-26 20:49:42

标签: c# wpf user-interface timer

我的应用程序出现问题,更改自定义属性后无法正确更新UI,该属性使用DisplayMemberBinding="{Binding property}"绑定到GridView列。

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>

这是我的自定义类,包含属性:

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

}
}

计时器:编辑以显示我正在尝试做的真实示例

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包含正在下载的文件的路径,使用FileStream进行编写。

我的目标是每秒读取文件大小并显示它。

问题:用户界面(在这种情况下是绑定到sizeProgress的列)仅在第一次打勾时更新一次,然后什么都没有。该应用程序仍然没有任何异常运行..

我真的不知道会出现什么问题。

如果您需要更多信息/代码告诉我。谢谢。

1 个答案:

答案 0 :(得分:1)

long BytesReceived = item.filePath.Length;

呃,这是包含文件路径的string的长度,而不是文件本身的长度。