views:

35

answers:

1

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.

+1  A: 

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.

AnthonyWJones
This doesn't work. I'm getting a syntax error; An object reference is required for the non-static field, method, or property 'System.Windows.Application.Resources.get'.Also, unless I misunderstand the FirePropertyChanged method would need to be called from within the view that implements the bindings and the resources would need to be in the application's resource dictionary, which is not the case; the resource names are keys provided to an external library that looks up the resources from a database.
Steve Crane
To elaborate a bit more on the scenario; what I have is an application composed of many views (user controls, etc.) that will all make use of this binding mechanism. In one of those views is a button that when clicked opens a translation UI (implemented in an assembly outside the scope of my application) within which the user may provide translations for one or many resources. It is when this UI closes (it notifies me) that I need to have a mechanism to fire off a refresh of all the bindings across all the views.
Steve Crane
I will have no idea which of the resources may have changed.
Steve Crane