Intercept any creation of a third-party internal class to modify a property

45 Views Asked by At

Firstly a bit of context. In my application I have a number of third party controls, one of these includes themes and style changes to normal WPF controls. The problem I have is that I also use a control from a different third party, within this control is an internal window.

The problem I have is that the first third party styles make the layout of this window all wrong.

What I am hoping to achieve is some way to intercept whenever this internal window is created and modify a property of it. If I can set the "ThemeName" property to "None" on this instance the style and layout of the window is fixed. I have tried and tested this using Snoop. I could also potentially intercept the Show() / ShowDialog() method of a Window and perform some checks?

From searching Castle seems to be recommend by all the examples I find require the class to configure which I can't do as it will fail to compile as the class is internal and therefore not accessible.

I'm not sure what code I can share unfortunately. I am able to get the type using Type.GetType which I can then use Activator.CreateInstance to create an instance, but I'm stuck on then setting the property and wiring up the interception.

I am not overly familiar with AOB.

1

There are 1 best solutions below

0
Ryan Thomas On BEST ANSWER

Okay, so it turns out I was drastically over complicating this and the actual fix turned out to be quite simple.

In the App.xaml.cs App() constructor I registered the following class handler to listen for any Window Loaded events.

EventManager.RegisterClassHandler(typeof(Window), Window.LoadedEvent, new RoutedEventHandler(WindowLoaded));

Then in that method I check the type of the Window and override the property as needed.

static void WindowLoaded(object sender, RoutedEventArgs e)
{
    if (sender.GetType().Name.Contains("InternalWindowClassName"))
        ThemeManager.SetThemeName(sender as DependencyObject, Theme.NoneName);
}

Can also use

(sender as DependencyObject).SetValue(DependencyProperty, value);