c# - 从事件更新 Viewmodel - PropertyChangedEventHandler 为空?

标签 c# wpf mvvm

我的 WPF 应用程序中有一个属性,我试图将其绑定(bind)到 UI。当我直接在 View 模型中更新属性时,它会按预期工作。但是,当我尝试使用事件从另一个类中更新此属性时,绑定(bind)似乎不起作用。经过仔细检查,我认为这是因为 PropertyChangedEventHandler 在发生这种情况时为空。

我在 ViewModel 中的属性:

public int BeatNumber
        {
            get
            {
                return beatNumber;
            }
            set
            {
                if (beatNumber != value)
                {
                    this.beatNumber = value;
                    RaisePropertyChanged(() => BeatNumber);
                }
            }
        }

ViewModel 中的事件:
public event EventHandler GetHtmlDone = delegate { };


public void GetHTML(string url)
{
    BeatNumber++;
}

通知对象:
public class NotificationObject : INotifyPropertyChanged
    {
        protected void RaisePropertyChanged<T>(Expression<Func<T>> action)
        {
            var propertyName = GetPropertyName(action);
            RaisePropertyChanged(propertyName);
        }

        private static string GetPropertyName<T>(Expression<Func<T>> action)
        {
            var expression = (MemberExpression)action.Body;
            var propertyName = expression.Member.Name;
            return propertyName;
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }



        public event PropertyChangedEventHandler PropertyChanged;
    }

当从另一个类调用我的事件时,如何更新此绑定(bind)?

最佳答案

通过使用命令而不是从事件中调用方法来解决它。

ICommand interface了解更多信息

关于c# - 从事件更新 Viewmodel - PropertyChangedEventHandler 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19199126/

相关文章:

c# - 匿名类的 ASP.NET MVC ViewBag 列表在 Count() 方法上抛出错误

c# - 将 char*[] 从 c++ dll 结构传递到 c#

.net - 在 Delphi 应用程序中从 COM 调用 WPF Windows

c# - 通过 WPF 命令模式启用提交/取消按钮

c# - 在 WinRT Prism/MVVM 中对 ComboBox 的 SelectedValue 进行数据绑定(bind)

c# - 数据表 - 对一行中的每个单元格求和

c# - 如何截取后台进程的屏幕截图?

c# - 如何基于 DataTemplate 的 DataContext 对象创建 ViewModel?

c# - MVVM:为 DataContract 类成员引发 PropertyChanged 事件

c# - ViewModel 和 Model 之间的 WPF 绑定(bind)