Search code examples
c#wpfxamlwindowsformsintegration

WPF Windows within Windows Forms as MDI where do I put the contents of App.xaml?


Due to circumstances beyond my control I must host some WPF screens inside a Windows Form app as an MDI child. I belive I know how to do this and have moved almost everything over. However I am not sure what to do with the Styles set in my App.xaml file.

How / where do I set a Global Styles reference?


Solution

  • since the App.xaml file is ignored in a WinForms app you will have to use a RecourceDictionary in watherver your UI root control is:

    <Page.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="myresourcedictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
    </Page.Resources>
    

    to include local resources you would frist create a ResourceDictionaryfor your local resources and then add the MergedDictionarieslike so:

    <Page.Resources>
      <ResourceDictionary>
        <!--local resources-->
        <Style x:Key="My Style">
        ...
        </Style>
    
        <!--"global" resources from file-->
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="myresourcedictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
    </Page.Resources>