0

我的应用程序有问题,它在更改自定义属性后无法正确更新 UI,该属性使用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>

这是我的带有属性的自定义类:

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包含正在下载的文件的路径,使用 aFileStream来写入。

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

问题: UI,在这种情况下是绑定到的列sizeProgress,只被更新一次,只是在第一次滴答时,然后什么也没有。该应用程序仍然运行无异常..

我真的不知道可能是什么问题。

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

4

1 回答 1

1
long BytesReceived = item.filePath.Length;

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

于 2013-08-26T23:36:36.770 回答