Home > Blockchain >  Using a WPF control template from a program library in a WPF project
Using a WPF control template from a program library in a WPF project

Time:06-08

I have a program library in which I have control templates for WPF controls. Now I want to include or use this program library as a .dll in another WPF project. Is it possible to use these control templates from the library in the XAML of the WPF project?

If yes, how do I do that?

CodePudding user response:

  • Define your control templates in a resource dictionary in a WPF Class Library or similar

  • Add a reference to the class library from the WPF application project

  • Reference the resource dictionary using a pack URI in the App.xaml.cs class of the WPF application:

      <Application x:Class="WpfApp1.App"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   StartupUri="MainWindow.xaml">
          <Application.Resources>
              <ResourceDictionary>
                  <ResourceDictionary.MergedDictionaries>
                      <ResourceDictionary Source="pack://application:,,,/YourClassLibrary;component/YourResourceDictionary.xaml"
                  </ResourceDictionary.MergedDictionaries>
              </ResourceDictionary>
          </Application.Resources>
      </Application>
    

You should then be able to reference any resource in the resource dictionary in the class library as usual.

  • Related