View does not receive notification when value of static property changes

I have a ViewModelBase class as follows:

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));
    }
}

Now I have another viewModel called GroupViewModel that inherits 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));
        }
    }
}

Now on the Groups.xaml page:

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

I have another ViewModel called MainWindowViewModel, I try to save CurrentGroup in a database as shown below, and then install CurrentGroup = new Group();, but in Group.xaml the TextBox text is not cleared:

Group group = GroupViewModel.CurrentGroup;
db.Groups.Add(group);
db.SaveChanges();
GroupViewModel.CurrentGroup = new Group();

Update:

If I use the code below in GroupViewModel, the output will be as expected. I mean, the View is updated when the Static property changes.

public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged
         = delegate { };
private static void NotifyStaticPropertyChanged(string propertyName)
{
   StaticPropertyChanged(null, new PropertyChangedEventArgs(propertyName));
}

ViewModelBase ( , GroupViewModel ViewModelBase), View . , NotifyStaticPropertyChanged public, , .

+3
2

Static PropertyChanged , :

public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged
         = delegate { };
private static void NotifyStaticPropertyChanged(string propertyName)
{
   StaticPropertyChanged(null, new PropertyChangedEventArgs(propertyName));
}

, , :

NotifyStaticPropertyChanged("CurrentGroup");

XAML, -

, WPF ClassName.PropertyName PropertyName.PropertyName.

, :

<Grid DataContext="{Binding Path=(local:GroupViewModel.CurrentGroup)}">
  .....
  .....
  <TextBlock Text="{Binding GroupName, TargetNullValue=''}" />
  .....
  .....
</Grid>

INPC .


UPDATE

ViewModelBase ( , GroupViewModel ViewModelBase), View , .

StaticPropertyChangedEvent , . , INotifyPropertyChanged, , .

MSDN, , , , , XAML StaticPropertyChangedEvent XAML.

, :

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));
} 

add, , , WPF , .

ViewModelBase, . WPF , .

+10

, , INotifyPropertyChanged . , , CurrentGroup, INotifyPropertyChanged .

, , CurrentGroup ( ), INotifyPropertyChanged CurrentGroup. , , @Silvermind , , .

+1

Source: https://habr.com/ru/post/1531993/


All Articles