问题描述:

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.

相关阅读:
Top