Home > database >  WPF Styles in a BASE class
WPF Styles in a BASE class

Time:02-12

I am working in WPF .Net 6 I have a Master.exe application and many DLLs, each of which contains functionality for every entity: Customers.dll | Bookings.dll | Vehicles.dll etc. In the each DLL I have WPF/XAML windows, view models, models, etc like a mini complete and isolated application.

I have also created a Base.dll that has the abstract classes that the models and viewmodels inherit. This works fine for a bit of shared code among all the dlls.

Similarly I want a one stop shop for uniform styling across all of the XAML windows.

The question now is; can I have XAML resources and styles in the Base.dll that are used by all the XAML windows in the other dlls?

CodePudding user response:

Sure. Create a WPF Custom Control Library project and define your common XAML resources in one or several resource dictionaries in this project, e.g.:

<!-- Dictionary1.xaml in WpfControlLibrary1.dll: -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="CommonBrush" Color="Red" />
</ResourceDictionary>

Then add a reference to this project from the project where you intend to use the common resources and merge the resource dictionary:

<ResourceDictionary>
    <Style x:Key="ButtonStyle" TargetType="Button">
        <Setter Property="Foreground" Value="{StaticResource CommonBrush}" />
    </Style>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Dictionary1.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
  • Related