C# 当静态属性的值更改时,视图未收到通知

C# 当静态属性的值更改时,视图未收到通知,c#,wpf,xaml,C#,Wpf,Xaml,我有一个ViewModelBase类,如下所示: public class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) {

我有一个ViewModelBase类,如下所示:

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public static event PropertyChangedEventHandler GlobalPropertyChanged = delegate { };
    public static void OnGlobalPropertyChanged(string propertyName, Type className)
    {
        GlobalPropertyChanged(className,new PropertyChangedEventArgs(propertyName));
    }
}
现在,我有了另一个名为GroupViewModel的viewModel,它继承了ViewModelBase:

public class GroupViewModel : ViewModelBase
{
    public GroupsViewModel()
    {
        CurrentGroup = new Group();
    }

    private static Group _currentGroup;
    public static Group CurrentGroup
    {
        get
        {
            return _currentGroup;
        }
        set
        {
            _currentGroup = value;
            OnGlobalPropertyChanged("CurrentGroup", typeof(Group));
        }
    }
}
现在在Groups.xaml页面中:

<Grid DataContext="{Binding CurrentGroup}">
    .....
    .....
    <TextBlock Text="{Binding GroupName, TargetNullValue=''}" />
    .....
    .....
</Grid>
更新:

如果我在GroupsViewModel中使用以下代码,则输出与预期一样。我的意思是当静态属性更改时视图会更新

public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged
         = delegate { };
private static void NotifyStaticPropertyChanged(string propertyName)
{
   StaticPropertyChanged(null, new PropertyChangedEventArgs(propertyName));
}
公共静态事件EventHandler StaticPropertyChanged
=委托{};
私有静态void NotifyStaticPropertyChanged(字符串propertyName)
{
StaticPropertyChanged(空,新PropertyChangedEventArgs(propertyName));
}

如果我在ViewModelBase中使用相同的代码(请注意GroupsViewModel继承了ViewModelBase),则当静态属性的值更改时,视图不会更新。此外,在本例中,我已将NotifyStaticPropertyChanged标记为public,以避免编译时错误,如关于保护级别的错误。

每当您要更新特定数据类型的属性更改时,都需要在该数据类型中实现
INotifyPropertyChanged
接口。这意味着,如果要更新视图模型的属性更改(在您的情况下是对
CurrentGroup
对象的更改),则需要在视图模型中实现
INotifyPropertyChanged
接口


但是,似乎您实际上想要更新在
CurrentGroup
类中所做的属性更改(以清除它们),因此在这种情况下,您还需要在
CurrentGroup
类中实现
INotifyPropertyChanged
接口。我相信这就是@Silvermind的意思,您需要引发与实例相关联的事件。

对于
静态属性更改
您必须在类中创建如下通用静态事件:

public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged
         = delegate { };
private static void NotifyStaticPropertyChanged(string propertyName)
{
   StaticPropertyChanged(null, new PropertyChangedEventArgs(propertyName));
}
但主要的问题是在XAML中绑定-

您将在名称空间、类和属性周围使用括号 因为WPF绑定引擎将路径解析为ClassName.PropertyName 而不是PropertyName.PropertyName

因此,它将是这样的:

<Grid DataContext="{Binding Path=(local:GroupViewModel.CurrentGroup)}">
  .....
  .....
  <TextBlock Text="{Binding GroupName, TargetNullValue=''}" />
  .....
  .....
</Grid>
在add上放置一个断点,您将看到它将被命中,因为WPF绑定引擎在内部钩住它以侦听静态属性更改事件


但只要将其移动到基类ViewModelBase,断点就不会被击中。因为WPF没有钩住它,所以属性中的任何更改都不会明显地更新UI。

尝试提供的解决方案。必须引发viewmodel的属性更改事件,静态成员不是实例的成员。@RohitVats好的,我会尝试一下并告诉您。@Silvermind必须引发viewmodel的属性更改事件是什么意思?@RohitVats我无法打开您提供的链接。它只是一直在加载……。我尝试使用上面的示例,但在xaml中我得到以下错误:
不支持嵌套类型:GroupViewModel.CurrentGroup
。另外,在我的问题中,我没有显示一些信息。我的页面的DataContext是local:GroupViewModel,然后正如您在qustion Grid中看到的,DataContext被设置为CurrentGroup,textBox的文本被绑定到GroupName。这和你在回答中提到的不一样吗?你把静态和实例属性混淆了。DataContext被设置为
GroupViewModel
的一个实例,但属性是静态的,所以您不能使用同样对XAML有效的实例对象访问它。您必须像我在更新的答案中那样设置DataContext。如果网格中还有其他项,只需将DataContext移动到TextBlock上,就完成了。现在我没有收到任何错误,但行为仍然相同。我的意思是,我的文本框还没有清理干净。我在一个小应用程序中试用了它,在我这边工作得很好。你们能在小应用程序中试试这个吗,或者至少让我知道制作它的确切步骤吗?你们可以在这里找到演示项目:但我在那个项目中得到一个错误:Key不能为null。参数名称键。
NotifyStaticPropertyChanged("CurrentGroup");
<Grid DataContext="{Binding Path=(local:GroupViewModel.CurrentGroup)}">
  .....
  .....
  <TextBlock Text="{Binding GroupName, TargetNullValue=''}" />
  .....
  .....
</Grid>
private static event EventHandler<PropertyChangedEventArgs> staticPC
                                                     = delegate { };
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged
{
   add { staticPC += value; }
   remove { staticPC -= value; }
}
protected static void NotifyStaticPropertyChanged(string propertyName)
{
   staticPC(null, new PropertyChangedEventArgs(propertyName));
}