You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF MVVM秒表功能视图不更新问题求助

问题解决:WPF秒表视图不更新

核心问题:PropertyChanged事件sender参数错误

你的NotifyPropertyChanged方法中,触发事件时传入的sender是null,而WPF的绑定机制需要sender指向ViewModel实例才能正确识别属性变更并更新视图。

修改NotifyPropertyChanged方法:

private void NotifyPropertyChanged([CallerMemberName] string name = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}

其他可选验证点

  • 调整DataContext赋值顺序:建议先调用InitializeComponent()再设置DataContext,确保XAML元素初始化完成后再绑定数据:
    public ProgressPage(ProjectInfo projectInfo)
    {
        InitializeComponent();
        DataContext = new ProgressViewModel(projectInfo);
    }
    
  • 检查ProjectInfo.CurrentStartedTime:确保CurrentStartedTime在Start方法中被正确赋值,后续没有被意外修改,避免流逝时间计算异常。
  • 确认定时器线程:你使用的DispatcherTimer默认在UI线程触发Tick事件,若后续修改定时器逻辑,需保证属性变更操作始终在UI线程执行。

内容的提问来源于stack exchange,提问作者Barta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 03:13:13