WPF计时器和UI更新问题
本文关键字:更新 新问题 UI 计时器 WPF | 更新日期: 2023-09-27 18:29:09
我的应用程序出现问题,它在更改自定义属性后没有正确更新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
进行写入
我的目标是每秒读取文件大小并显示它
问题:UI,在本例中是绑定到sizeProgress
的列,只更新了一次,只在第一次勾选时更新,然后什么都不更新。应用程序仍然运行,没有任何异常。。
我真的不知道问题出在哪里。
如果您需要更多信息/代码,请告诉我。谢谢。
long BytesReceived = item.filePath.Length;
呃,这是包含文件路径的string
的长度,而不是文件本身的长度。