So,
Having examined all the possible ways, I introduced a new behavior that fires an event every time a particular resource changes WPF.
The source can be downloaded or cloned from https://github.com/jeromerg/ResourceChangeEvent .
public class ResourceChangeNotifierBehavior
: System.Windows.Interactivity.Behavior<FrameworkElement>
{
public static readonly DependencyProperty ResourceProperty
= DependencyProperty.Register("Resource",
typeof(object),
typeof(ResourceChangeNotifierBehavior),
new PropertyMetadata(default(object), ResourceChangedCallback));
public event EventHandler ResourceChanged;
public object Resource
{
get { return GetValue(ResourceProperty); }
set { SetValue(ResourceProperty, value); }
}
private static void ResourceChangedCallback(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs args)
{
var resourceChangeNotifier = dependencyObject as ResourceChangeNotifierBehavior;
if (resourceChangeNotifier == null)
return;
resourceChangeNotifier.OnResourceChanged();
}
private void OnResourceChanged()
{
EventHandler handler = ResourceChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
}
So that the event handler OnResourceChangedcan be hooked in the file XAMLas follows:
<i:Interaction.Behaviors>
<Behaviours:ResourceChangeNotifierBehavior
Resource="{DynamicResource MyDynamicResourceKey}"
ResourceChanged="OnResourceChanged"/>
</i:Interaction.Behaviors>
Hope this helps ...
source
share