views:

21

answers:

1

I know that I can rebind all instances of a specific property for a specific type of element, as in this method that rebinds the Text property of all Textblocks.

public void Rebind()
{
  foreach (var textBlock in LayoutRoot.GetDescendents().OfType<TextBlock>())
  {
    BindingExpression bindExp = textBlock.GetBindingExpression(TextBlock.TextProperty);
    if (bindExp != null)
    {
      Binding bind = bindExp.ParentBinding;
      textBlock.SetBinding(TextBlock.TextProperty, bind);
    }
  }
}

What I want to be able to do though is rebind all properties that have bindings for all elements in the visual tree. More specifically I would like to rebind all bindings that use a specific value converter. How might I do so?

+1  A: 

This isn't realistically acheivable since FrameworkElement provides no way to enumerate the set of binding expressions that currently apply to it.

In order to achieve this you would need to have first collected all the dependency properties that may apply (at least at a per element type but that adds further complications) and then attempt GetBindingExpression on each per element. Real ugly and real slow.

Time to design this requirement out.

AnthonyWJones
That's what I thought. I want to use bindings that are not bound to any property, but use a value converter and its parameter to look up a resource (not from ResX files) and return it, ignoring the value. The resources can change while the application is running (a user can provide translations) and I would like to rebind when one or more resources change.
Steve Crane
@Steve: Since you are not binding to a property you have a work around. Create a dummy class that implements `INotifyPropertyChanged` implement a single dummy property on it. Add an instance of this Dummy to the `Application.Resources` and bind __ALL__ of these bindings to it `{Binding Dummmy, Source={StaticResource DummyObject}}, Converter={your converter etc.` Now when you want to update your bindings just get your Dummy object to fire the `PropertyChanged` event for the Dummy property.
AnthonyWJones
Thanks Anthony, that sounds like the perfect solution.
Steve Crane
I have one problem though. I would like a static method in the dummy class that I can use to trigger the property change notification from anywhere but the PropertyChanged event is not static and can't be made static as it requires an instance handle be passed as sender. How could I approach this?
Steve Crane
@Steve: `public static void FirePropertyChanged(string key) { ((Dummy)Application.Resources[key]).NotifyPropertyChanged("Dummy") }`
AnthonyWJones