Philip Hendry's Blog

Making WPF Styles work in a WinForms Application

Jun 4, 2014 • • 1 min read

History… skip if you want the detail!

One of the .NET projects I work on is almost 10 years old now and it’s starting to show! In particular the WinForms client application is starting to creak under the strain of having so many developers work on it over the years leaving the code behind the forms quite a mess.

For this reason we’ve made steps to introduce an MVVM pattern into the old code which separates and pushes a lot of complexity back into the ViewModel and the Model. However, this still left WinForms requiring quite a bit of code-behind to manage the binding between the form and the ViewModel. But this was all for a reason… with any luck we could remove the WinForm and Code-Behind and replace with a single XAML file declaratively bound to the ViewModel.

Styles did not apply

And that’s when we met one of a number of problems. We’ve already had to tackle adding resources to a WinForm host but we were left wondering why the styles wouldn’t apply in the designer.

The solution was to create a new WPF application assembly which meant the App.xaml was being interpreted by the designer although we still added a static method to initialise the resources when the WinForms app started:

public partial class App
  public static void Initialise()
     if (Current == null)
        new Application();
        var resourceLocator = new Uri("WpfAssembly;component/resources/ControlStyles.xaml",
                LoadComponent(resourceLocator) as ResourceDictionary);
Post by: Philip Hendry