开发者

How can I trigger the PropertyChanged event from static method?

开发者 https://www.devze.com 2023-01-07 01:55 出处:网络
I have the following class public class LanguagingBindingSource : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged;

I have the following class

public class LanguagingBindingSource : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  public string Dummy
  {
    get { return String.Empty; }开发者_开发问答
    set
    {
      PropertyChanged(this, new PropertyChangedEventArgs("Dummy"));
    }
  }
}

that is bound to elements in XAML like this

Text="{Binding Dummy,Source={StaticResource languageSource},Converter={StaticResource languageConverter},ConverterParameter=labelColor}"

The sole purpose of the LanguageBindingSource class and its Dummy method is to allow property notifications to update the bindings when one or more resources change. The actual bound values are provided by the converter, looking up resources by the names passed as parameters. See the comments on this answer for more background.

My problem is that the resources are changed by a process external to the XAML pages containing the bindings and I need a single static method that I can call to trigger property change notification for all instances of the binding. I'm struggling to figure out just how I might do that. All ideas will be most appreciated.


Modify your class as follows:-

public class LanguagingBindingSource : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged = delegate {};

  public static void FirePropertyChanged(string key)
  {
     ((LanguagingBindingSource)Application.Resources[key]).NotifyPropertyChanged("Dummy");
  }

  private void NotifyPropertyChanged(string name)
  {
    PropertyChanged(this, new PropertyChangedEventArgs(name);
  }

  public string Dummy
  {
    get { return String.Empty; }
    set
    {
      NotifyPropertyChanged("Dummy"));
    }
  }
}

Now are any point where you need to fire off this change use:-

LanguagingBindingSource.FirePropertyChanged("languageBindingSource");

Where "languageBindingSource" is the resource key that you are also using in your binding Source property.

0

精彩评论

暂无评论...
验证码 换一张
取 消