0

I have an application with many Windows, and I want to use the same templates and styles in all of these windows.

Warning: This is NOT a "WPF Application", it's a "class library" using WPF windows. I do not have an "App.xaml" file!!!

What is the easiest way to do that in WPF?

Currently, I have this in a Window, working fine:

<Window.Resources>
    <ControlTemplate x:Key="ErrorTemplate">
        <Grid  ToolTip="{Binding Path=/ErrorContent}">
            ....
        </Grid>
    </ControlTemplate>

    <Style TargetType="Control" x:Key="ErrorStyle">
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        <Style.Triggers>
            ... 
        </Style.Triggers>
    </Style>
</Window.Resources>

And I use it as

<TextBox Style="{StaticResource ErrorStyle}" .../>

But this is clearly declared in a single Window. How do I declare it in a way that it could be used by many windows (considering I do not have an App.xaml file)

1
  • Regarding your edit, it doesn't matter. You can still use ResourceDictionary and merge them wherever you need them. Commented Sep 19, 2022 at 13:16

1 Answer 1

1

Do it in the App.Xaml, will be available for all items in the application

Alternatively you can create ResourceDictionaries and merge those in the App.Xaml or anywhere you need them

DOCS:

Application.Resources https://learn.microsoft.com/en-us/dotnet/desktop/wpf/systems/xaml-resources-how-to-use-application?view=netdesktop-6.0

Merged Dictionaries https://learn.microsoft.com/en-us/dotnet/desktop/wpf/systems/xaml-resources-merged-dictionaries?view=netdesktop-6.0

1
  • It's generally best to post a brief but helpful example/summary as links can die at any time, which would make a complete answer that only lists external references pretty useless.
    – BionicCode
    Commented Sep 19, 2022 at 13:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.