WPF: is there an event caused by a resource change

is there a way to get notified of changes in the cost of a particular WPF resource?

We need to dynamically adapt the font size of the content in the WPF application ... For WPF controls, we set it Control.FontSizeto a dynamic resource, and the font changes automatically. Unfortunately, we also have a built-in winforms control whose font size cannot be set this way. The idea was to subscribe to an event that was triggered each time a resource value was changed, and implement a custom update to the winforms control. Any suggestion?

Thank you in advance!

+4
source share
2 answers

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 ...

+4
source

I had the same problem, but I really wanted to get a notification if any resource changed. In my case, a was DynamicResourceused on some DependencyObjectthat was not in the visual tree. Therefore, the property was not updated automatically if the resource changed.

, jeromerg, ResourceChanged:

public static void NotifyOnResourcesChanged(this FrameworkElement element, EventHandler onResourcesChanged)
{
    ResourcesChangedEvent.AddMethod.Invoke(element, new object[] { onResourcesChanged });
}
private static readonly EventInfo ResourcesChangedEvent = typeof(FrameworkElement).GetEvent("ResourcesChanged", BindingFlags.Instance | BindingFlags.NonPublic);

InvalidateProperty, .

0

Source: https://habr.com/ru/post/1534858/


All Articles